2013-08-19 69 views
0

我試圖創建一個宏,它將搜索文檔並突出顯示每個出現的空格,單詞「for」,然後像這樣的另一個空間「for」使用修改後的代碼這個網站,我已經得到了這一點:在Microsoft Word的VBA宏中突出顯示文本

Sub findfunction() 
If (findHL(ActiveDocument.Content, "[ for ]")) = True Then MsgBox "Highlight Comma's and Coordinating Conjunctions Done", vbInformation + vbOKOnly, "Highlight Result" 
End Sub 

Function findHL(r As Range, s As String) As Boolean 
Options.DefaultHighlightColorIndex = wdYellow 
r.Find.Replacement.Highlight = True 
r.Find.Execute MatchWholeWord:=True, FindText:=s, MatchWildcards:=True, Wrap:=wdFindContinue, Format:=True, replacewith:="", Replace:=wdReplaceAll 
findHL = True 
End Function 

的問題是,它強調只是字母F,O,和R的每次出現。我希望它只在發現序列「for」時突出顯示,而不是單個字符。我對此很陌生,我不確定該從哪裏出發,所以我們將不勝感激。謝謝:D

+0

檢查[通配符規則](http://www.gmayor.com/replace_using_wildcards.htm) –

回答

0

通配符不是必需的。搜索字符串應該是「for」和MatchWildcards:= False。

Sub findfunction() 
    If (findHL(ActiveDocument.Content, " for ")) = True Then 
     MsgBox "Highlight Comma's and Coordinating Conjunctions Done", vbInformation + vbOKOnly, "Highlight Result" 
    End If 
End Sub 

Function findHL(r As Range, s As String) As Boolean 
    Options.DefaultHighlightColorIndex = wdYellow 
    r.Find.replacement.Highlight = True 
    r.Find.Execute MatchWholeWord:=True, FindText:=s, MatchWildcards:=False, Wrap:=wdFindContinue, Format:=True, replacewith:="", Replace:=wdReplaceAll 
    findHL = True 
End Function