使用VBA将括号移到Microsoft Word中的本地范围内注释

问题描述 投票:0回答:1

我试图将括号中的所有文本移动到我选择的范围内的注释中。我几乎在那里,但由于某种原因它只能在范围的开始。我的宏如下:

Sub CommentOutParenthsLocal()
'
' CommentBubble Macro
'
'
Dim myRange As Range
Set myRange = Selection.Range
searchText = "\(*\)"

With myRange.Find
    .MatchWildcards = True
    Do While .Execute(findText:=searchText, Forward:=True) = True
      ActiveDocument.Comments.Add myRange, myRange.Text
      myRange.Text = ""
    Loop
 End With
End Sub

有什么建议?

vba ms-word word-vba
1个回答
2
投票

根据您的描述,您需要将代码的范围限制为您实际选择的范围,以及其他内容。在这种情况下,请尝试:

Sub CommentOutParenthsLocal()
Application.ScreenUpdating = True
Dim myRange As Range
Set myRange = Selection.Range
With Selection.Range
  With .Find
    .Text = "\(*\)"
    .Forward = True
    .MatchWildcards = True
    .Execute
  End With
  Do While .Find.Found = True
    If .InRange(myRange) = False Then Exit Do
    .Comments.Add .Duplicate, .Text
    .Text = vbNullString
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = False
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.