Word VBA 查找并突出显示匹配项
我的下面的代码已经可以工作,但仍需要微调。它的功能是查找通配符搜索字符串的匹配项并突出显示出现的情况。但我相信仍然可以使用全部替换在一行中完成。我已经尝试了几乎所有我能想到的方法,我认为是时候向专家询问这个问题了。请告诉我如何以更短的方式完成此操作。任何帮助将不胜感激。谢谢!
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
隐藏在谷歌深处:
Hidden very deep inside google:
经过一些试验,我设法找到了自己的解决方案。这是我的解决方案,仅供那些可能正在为我之前的问题寻找相同解决方案的其他人参考:
简单,但它将我之前的代码精简为几行。
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:
Simple yet it stripped down my previous code to a few lines.