使用VBA在Microsoft Word中移动括号以在本地范围内进行注释

时间:2019-03-10 23:56:27

标签: vba ms-word word-vba

我正在尝试将括号中的所有文本移到所选范围内的注释中。我快到了,但由于某种原因,它仅在范围开始时起作用。我的宏如下:

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

有什么建议吗?

1 个答案:

答案 0 :(得分: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