Word VBA 通配符搜索匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5577453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:58:06  来源:igfitidea点击:

Word VBA Wildcard Search Match

vbams-wordwildcardword-vba

提问by decrementor

How can we extract the find result for a wildcard search in a range?

我们如何提取范围内通配符搜索的查找结果?

dim r as range
set r = activedocument.range

Do While r.Find.Execute(findtext:="<*>", MatchWildcards:=True) = True
     Msgbox <Show the matching word it found here>
     if **<the word it matched>** = "Stop" then do something here
Loop 

The above code uses range to find any whole word in a range by using <*> as the wildcard pattern. But how can i get the current match/word it found?

上面的代码使用 range 通过使用 <*> 作为通配符模式来查找范围内的任何整个单词。但是我怎样才能得到它找到的当前匹配/单词?

采纳答案by shahkalpesh

Sub test()
Dim r As Range
Set r = ActiveDocument.Range
r.Select

With Selection.Find
    .ClearFormatting
    .Text = "<*>"
    .Forward = True
    .Wrap = wdFindStop
    .MatchWildcards = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = True
End With

Do While Selection.Find.Execute
     If Selection.Text = "stop" Then MsgBox "wohoo"
Loop
End Sub

EDIT: I am not too familiar with word object model. The Findmethod works on Rangebut I don't know, how to find the text it could find. The above code is modified after running a macro, to see what output does find produce.

编辑:我对 word 对象模型不太熟悉。该Find方法有效,Range但我不知道如何找到它可以找到的文本。上面的代码是在运行宏后修改的,以查看找到的输出结果。

Hope that helps.

希望有帮助。