在 Word 中运行多个单词搜索宏

在 Word 中运行多个单词搜索宏

因此,我正在运行此宏来一次查找并突出显示多个单词。但是,我想将其设置为仅查找整个单词,因为它当前正在查找单词内的字母并突出显示它们。例如,如果我只想突出显示“as”,它也会突出显示“wasp”中的 as。有人能帮我修改仅显示整个单词的代码吗?

Sub HiLightList()
Application.ScreenUpdating = False
Dim StrFnd As String, Rng As Range, i As Long
StrFnd = "as,the,bold"
For i = 0 To UBound(Split(StrFnd, ","))
  Set Rng = ActiveDocument.Range
  With Rng.Find
    .ClearFormatting
    .Text = Split(StrFnd, ",")(i)
    .Replacement.ClearFormatting
    .Replacement.Highlight = True
    .Replacement.Text = "^&"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = True
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = True
    .Execute Replace:=wdReplaceAll
  End With
Next
Set Rng = Nothing
Application.ScreenUpdating = True
End Sub

答案1

要搜索整个单词,请在“查找”代码中添加以下行:

 .MatchWholeWord = True

参考 : Find.MatchWholeWord 属性(Word)Find.MatchWholeWord property (Word)

相关内容