【问题标题】:Word VBA Macro to comment out nested parenthesisWord VBA宏注释掉嵌套括号
【发布时间】:2020-08-29 00:22:02
【问题描述】:

我有一个宏如下:

Sub CommentOutParenthsLocal()
'
' CommentBubble Macro
'
'
Dim myRange As Range
Set myRange = Selection.Range
Set oScope = myRange.Duplicate

searchText = "\(*\)"

With myRange.Find
    .MatchWildcards = True
    Do While .Execute(findText:=searchText, Forward:=True) = True
        If myRange.InRange(oScope) Then
            If Len(myRange.Text) > 4 Then
                ActiveDocument.Comments.Add myRange, myRange.Text
                myRange.Text = ""
            End If
        Else
            Exit Do
        End If
    Loop
 End With
End Sub

但是,如果我有嵌套括号,例如This is my (nested parenthesis (document ) in full),这将不起作用

它将匹配第一个右括号而不是最外面的括号。有没有办法写一个匹配的正则表达式?

【问题讨论】:

    标签: regex vba ms-word


    【解决方案1】:

    试试:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim Rng As Range
    With Selection
      Set Rng = .Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "\([!\(]@\)"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchWildcards = True
        .Execute
      End With
      Do While .Find.Found
        If Not .InRange(Rng) Then Exit Do
        If Len(.Text) > 4 Then
          .Comments.Add .Range, .Text
          .Text = ""
        End If
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    End Sub
    

    【讨论】:

      【解决方案2】:

      作为建议,请尝试在每个模块/类/表单的开头使用 Option Explicit。这将阻止您使用尚未声明的变量。

      下面的代码会减少

      This is my (nested parenthesis (document ) in full)
      

      This is my
      

      (nested parenthesis (document ) in full)
      

      作为评论添加。

      Option Explicit
      
      Sub CommentOutParenthsLocal()
      '
      ' CommentBubble Macro
      '
      '
      Dim myRange As Range
      Set myRange = Selection.Range
      
      Dim oScope As Word.Range
      Set oScope = myRange.Duplicate
      
      Dim searchtext As String
      searchtext = "\(*\)"
      
          With myRange.Find
      
              .MatchWildcards = True
      
              Do While .Execute(findText:=searchtext, Forward:=True) = True
      
                  myRange.Select
      
                  If myRange.InRange(oScope) Then
      
                      Dim myCount As Long
                      Dim myText As String
                      myText = myRange.Text
                      myCount = Len(myText) - Len(Replace(myText, "(", vbNullString)) - 1
      
                      Do Until myCount = 0
      
                          myRange.MoveEndUntil cset:=")"
                          myRange.MoveEnd Count:=1
                          myCount = myCount - 1
      
                      Loop
      
                      If Len(myRange.Text) > 4 Then
      
                          ActiveDocument.Comments.Add myRange, myRange.Text
                          myRange.Text = ""
      
                      End If
                  Else
      
                      Exit Do
      
                  End If
                  myRange.Start = myRange.End + 1
                  myRange.End = oScope.End
      
              Loop
      
           End With
      
      End Sub
      

      【讨论】:

      • 好的,我认为您的代码有错误。尝试“ActiveDocument.Comments.Add myRange.duplicate, myRange.Text”
      • 出于某种原因,它仅在我按“command+z”撤消时才会显示。不是立即 - 甚至发布您的更改。
      • 感觉就像当它折叠范围时,它会覆盖它。
      • 将 .duplicate 添加到 myrange 后,代码可以在我的电脑上找到。在评论选项卡中,显示标记我必须在 cmets 出现之前选择 show cmets。
      猜你喜欢
      • 2012-05-27
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多