2011-04-07 302 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