使用宏查找首字母缩略词,除非它后跟 ( in Word/VBA
Use macro to find acronyms unless it is followed by ( in Word/VBA
我知道我可以使用通配符在 Word 文档中查找首字母缩略词。我有一个这样做的宏。但是,我想 'unhighlight' 这个首字母缩写词,如果它后面跟着一个 (。例如,我的内容可能会说 "BRB (be right back)" 所以 BRB 不会被突出显示。但是,大声笑是因为(大笑)没有立即跟随文本。
我正在努力避免宏引发的许多误报。有什么办法可以排除 'BRB' 结果吗?
With ActiveDocument.Content.Find
.ClearFormatting
.Text = "<[A-Z]{2,}>"
With .Replacement
.Text = "^&"
.ClearFormatting
.Highlight = True
End With```
试试这个:
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
我知道我可以使用通配符在 Word 文档中查找首字母缩略词。我有一个这样做的宏。但是,我想 'unhighlight' 这个首字母缩写词,如果它后面跟着一个 (。例如,我的内容可能会说 "BRB (be right back)" 所以 BRB 不会被突出显示。但是,大声笑是因为(大笑)没有立即跟随文本。
我正在努力避免宏引发的许多误报。有什么办法可以排除 'BRB' 结果吗?
With ActiveDocument.Content.Find
.ClearFormatting
.Text = "<[A-Z]{2,}>"
With .Replacement
.Text = "^&"
.ClearFormatting
.Highlight = True
End With```
试试这个:
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