2011-04-07 303 views
1

我们如何在一个范围内提取通配符搜索的查找结果?Word VBA通​​配符搜索匹配

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 

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

回答

1
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 

编辑:我不太熟悉单词对象模型。 Find方法适用于Range,但我不知道如何找到它可以找到的文本。上面的代码在运行一个宏之后被修改,以查看哪些输出确实会产生。

希望有所帮助。

+0

这也将工作shakalpesh谢谢你的洞察力。然而,我发现了简单的解决方案。我只需要使用r.text来提取循环中的当前查找结果。希望这可以帮助其他人在范围内的通配符搜索。 ;) – decrementor 2011-04-09 06:28:29