Word VBA 查找并突出显示匹配项

发布于 2024-10-23 14:06:28 字数 673 浏览 5 评论 0原文

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

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

i have the codes below that is already working but still needs to be fine tuned. Its a function that finds matches for a wildcard search string and highlights the occurences. But i believe that it can still be done in a single line using replace all. I have tried almost everything that i could possibly think of and i think its time to ask the experts about this. Please show me how this can be done in a yet shorter way. Any help will be greatly appreciated. Thanks!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丶视觉 2024-10-30 14:06:28

隐藏在谷歌深处:

Options.DefaultHighlightColorIndex = wdYellow

Selection.find.HitHighlight( string ) 

Hidden very deep inside google:

Options.DefaultHighlightColorIndex = wdYellow

Selection.find.HitHighlight( string ) 
烟沫凡尘 2024-10-30 14:06:28

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

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

简单,但它将我之前的代码精简为几行。

I managed to find my own solution doing a few trials. Here is my solution just for reference to others who might be looking for the same solution to my previous problem:

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

Simple yet it stripped down my previous code to a few lines.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文