【问题标题】:Word VBA Wildcard Search MatchWord VBA 通配符搜索匹配
【发布时间】:2011-04-07 07:27:31
【问题描述】:

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

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 

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

【问题讨论】:

    标签: vba wildcard ms-word


    【解决方案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 但我不知道如何找到它可以找到的文本。上面的代码是在运行一个宏之后修改的,看看 find 会产生什么输出。

    希望对您有所帮助。

    【讨论】:

    • 这也可以工作 shakalpesh 感谢您的洞察力。然而,我找到了简单的解决方案。我只需要使用 r.text 在循环中提取当前的查找结果。希望这也可以帮助其他人在一个范围内进行通配符搜索。 ;)
    猜你喜欢
    • 1970-01-01
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-06
    • 1970-01-01
    相关资源
    最近更新 更多