开发者

Word VBA Wildcard Search Match

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 whol开发者_开发技巧e word in a range by using <*> as the wildcard pattern. But how can i get the current match/word it found?


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 Find method works on Range but 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.

Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜