0

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
4

1 回答 1

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

代码经过试验和测试,运行良好。

于 2013-07-15T17:17:17.277 回答