有没有一种方法可以突出显示MS Word文本框中的特定单词?

时间:2019-10-21 23:28:34

标签: vba ms-word

我发现的当前VBA代码突出显示了放在常规段落中的列表中的特定单词。但是,我还需要突出显示放置在文本框中的单词。还有关于如何突出显示文本框内文本的想法吗?

Sub HighlightWords()
'
' HighlightWords Macro
'
'
Dim Word As range
Dim WordCollection(2) As String
Dim Words As Variant

WordCollection(0) = "Andres"
WordCollection(1) = "just"
WordCollection(2) = "Pending"

Options.DefaultHighlightColorIndex = wdYellow
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = True

For Each Word In ActiveDocument.Words
For Each Words In WordCollection
With Selection.Find
.Text = Words
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False

End With
Selection.Find.Execute Replace:=wdReplaceAll

Next

Next
End Sub

1 个答案:

答案 0 :(得分:0)

您应该使用此循环:

For i = 1 To ActiveDocument.Shapes.Count
    ActiveDocument.Shapes(i).Select

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Highlight = True

    ' do something...
Next i