【问题标题】:Highlighting Text in VBA macro for Microsoft Word在 Microsoft Word 的 VBA 宏中突出显示文本
【发布时间】:2013-08-20 18:56:43
【问题描述】:

我正在尝试创建一个宏,它将搜索文档并突出显示每个出现的空格,单词“for”,然后使用来自该站点的修改代码,然后像这样的另一个空格“for”,我得到了这个:

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

【问题讨论】:

标签: vba replace highlight ms-word


【解决方案1】:

通配符不是必需的。搜索字符串应该是 "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

【讨论】:

    猜你喜欢
    • 2018-10-03
    • 2012-01-26
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    • 2017-08-10
    • 1970-01-01
    • 2013-10-17
    相关资源
    最近更新 更多