【问题标题】:Use macro to find acronyms unless it is followed by ( in Word/VBA使用宏查找首字母缩略词,除非它后跟 ( 在 Word/VBA 中
【发布时间】:2019-12-09 06:30:08
【问题描述】:

我知道我可以使用通配符在 Word 文档中查找首字母缩略词。我有一个这样做的宏。但是,如果后面跟着一个 (,我想“取消突出显示”该首字母缩写词。例如,我的内容可能会说“BRB(马上回来)”,所以 BRB 不会被突出显示。但是,LOL 会是因为(大笑) 不会紧跟在文本后面。

我试图避免宏引发的大量误报。有没有办法可以排除“BRB”结果?

With ActiveDocument.Content.Find
  .ClearFormatting
  .Text = "<[A-Z]{2,}>"
With .Replacement
  .Text = "^&"
  .ClearFormatting
  .Highlight = True
End With```

【问题讨论】:

  • 你做了三组替换。首先将 BRB ( 替换为 BRB XX,然后您进行查找并替换该未突出显示的部分,然后将 BRB XX 替换为 BRB (

标签: vba ms-word wildcard


【解决方案1】:

试试这个:

Sub HighlightAcronyms()

    Dim rng As Range, r2 As Range

    Set rng = ActiveDocument.Content
    Set r2 = ActiveDocument.Content

    With rng.Find
        .ClearFormatting
        .Text = "<[A-Z]{2,}>"
        .Forward = True
        .Wrap = wdFindStop
        .MatchCase = True
        .MatchWildcards = True
        .Format = False

        Do While .Execute
            'look two characters past the found acroynm
            r2.Start = rng.End + 1
            r2.End = rng.End + 3
            Debug.Print rng.Text, r2.Text

            'highlight if r2 has a "(" otherwise clear highlight
            rng.HighlightColorIndex = IIf(r2.Text Like "*(*", _
                                        wdAuto, wdYellow)


        Loop
    End With

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多