2013-08-19 70 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