【问题标题】:Word VBA Find and Highlight matchesWord VBA 查找并突出显示匹配项
【发布时间】:2015-02-25 08:50:10
【问题描述】:

我有下面的代码已经在工作,但仍需要微调。它是一个查找通配符搜索字符串的匹配项并突出显示出现的函数。但我相信它仍然可以使用全部替换在一行中完成。我已经尝试了几乎所有我能想到的东西,我认为是时候询问专家了。请告诉我如何以更短的方式做到这一点。任何帮助将不胜感激。谢谢!

Sub findfunction()
If (findHL(activedocument.Range, "[aeiou]")) = True Then MsgBox "Highlight vowels Done", vbInformation + vbOKOnly, "Vowels Highlight Result"
End Sub

Function findHL(r As Range, s As String) As Boolean
Dim rdup As Range
Set rdup = r.Duplicate
rdup.Find.Wrap = wdFindStop

Do While rdup.Find.Execute(findtext:=s, MatchWildcards:=True) = True
   If (Not rdup.InRange(r)) Then Exit Do
   rdup.HighlightColorIndex = wdBlue
   rdup.Collapse wdCollapseEnd
Loop

findHL = True
End Function

【问题讨论】:

    标签: vba ms-word


    【解决方案1】:

    隐藏在谷歌的深处:

    Options.DefaultHighlightColorIndex = wdYellow
    
    Selection.find.HitHighlight( string ) 
    

    【讨论】:

      【解决方案2】:

      经过几次试验,我设法找到了自己的解决方案。这是我的解决方案,仅供参考,其他人可能正在为我之前的问题寻找相同的解决方案:

      Sub findfunction()
      If (findHL(activedocument.Content, "[aeiou]")) = True Then MsgBox "Highlight vowels Done", vbInformation + vbOKOnly, "Vowels Highlight Result"
      End Sub
      
      Function findHL(r As Range, s As String) As Boolean
      Options.DefaultHighlightColorIndex = wdBlue
      r.Find.Replacement.highlight = True
      r.Find.Execute FindText:=s, MatchWildcards:=True, Wrap:=wdFindContinue, Format:=True,  replacewith:="", replace:=wdReplaceAll
      findHL = True
      End Function
      

      简单但将我之前的代码精简到几行。

      【讨论】:

      • 感谢您的回答。 “highlight”属性是否适用于屏幕视图或文档内容。换句话说,它是视觉线索,还是文档的一部分(在这种情况下是打印的)。我正在寻找一种向用户突出显示某些内容的方法。
      猜你喜欢
      • 2018-10-03
      • 2018-10-21
      • 1970-01-01
      • 1970-01-01
      • 2011-07-12
      • 2015-07-29
      • 2019-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多