Word/VBA 查找..使用通配符替换并转义

Word/VBA 查找..使用通配符替换并转义

我想通过 VBA 格式化(例如突出显示)Word 文档中的特殊单词。通常情况下,这种方法效果很好,但在这种情况下,我需要找到类似 <*> 的东西(用“<”和“>”括号括起来的单词,其中 * 是通配符,但括号不是。我尝试通过反斜杠“转义”括号,但不起作用。我收到运行时错误 5623:替换文本包含超出范围的组号。

wrdDocResults.Select
With Selection.Find
    .Text = "\<*\>"
    .Replacement.Font.Color = wdColorBlue 
    .Forward = True           
    .Wrap = wdFindStop          
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = True
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll   

感谢您的帮助!

答案1

试试这个

Sub test()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
    .Text = "\<*\>"
    .Replacement.Font.Color = wdColorBlue
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = True
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

相关内容