Vba 有没有办法突出显示MS Word文本框中的特定单词?

Vba 有没有办法突出显示MS Word文本框中的特定单词?,vba,ms-word,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) = "jus

我找到的当前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

您应该使用以下循环:

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

文本框中的文本格式可以与普通文档中的文本格式相同。现在,您需要做的就是研究如何定位特定的文本框。有关此代码的几点说明:1)我看不出循环
ActiveDocument的原因。Words
-所有这一切只需重复另一个循环并查找文档中的每个单词,这是完全不必要和耗时的。2) 在使用的VBA对象模型中,声明与关键字同名的变量是一个非常糟糕的想法。Word VBA将
单词
单词
作为特殊事物使用-这可能会导致实际问题。