更改所选句子中某些单词的字体

时间:2013-07-15 14:58:45

标签: vba word-vba

Forward := True只会将myArray中单词的颜色从光标位置更改为文件末尾。 我只想申请选择的句子。我没找到命令。有什么建议?提前谢谢。

Dim rng As Word.range
Dim i As Long
Dim myArray

myArray = Array("FROM", "ADD", "MAYBE"......)
For i = 0 To UBound(myArray)
Set rng = Selection.range
With rng.Find
    .Text = myArray(i)
    .Format = True
    .MatchCase = False
    .MatchWholeWord = True
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False

Do While .Execute (Forward := True) = True
    rng.Font.Color = RGB(100, 150, 255)
Loop
End With

Next
End Sub

1 个答案:

答案 0 :(得分:0)

在您的情况下,您需要使用不同类型的替换部分代码。因此,而不是代码的这一部分:

With rng.Find
'... remove everything within With...End With structure
End with

你应该使用类似这样的东西:

With rng.Find
    .Text = myArray(i)
    .Format = False
    .MatchCase = False
    .MatchWholeWord = True

    With .Replacement.Font
        .Color = RGB(100, 150, 255)
    End With
    .Execute Forward:=False, Replace:=wdReplaceAll
End With

代码经过测试,并且工作正常。