2013-07-15 29 views
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 

回答

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 

代碼爲久經考驗和它的正常工作。

+0

3.它匹配'FROM',''FROM'','&&& FROM' ...我只需要匹配第一個'FROM',沒有任何標點符號。再次感謝。 – user2502240

+0

謝謝! 1.我不認爲在倒數第二行中有'Forward:= False'&'With .Replacement.Font .Color = RGB(100,150,255) End With With' is necessary。那麼'.Replacement.Font.Color = RGB(100,150,255)'? 2.我不明白爲什麼你將'.Format'設置爲'False' – user2502240

+0

對於點1和2做了一些實驗,如果有什麼與其他設置一起使用的話可以使用它們。我提供了可用的代碼。通常有兩種(或更多)的方式來獲得你需要的VBA。關於第3點,我可以看到很多可能的情況,因此您需要自己用[這個有用的鏈接](http://www.gmayor.com/replace_using_wildcards.htm) –

相關問題