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