使用通配符在文档中查找字符串;返回完整的字符串 VBA

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19376093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:49:37  来源:igfitidea点击:

Find string in doc using wildcards; return full string VBA

excelexcel-vbasearchms-wordword-vbavba

提问by Benjamin Dover

What I need to do is search a word document for a dollar amount and then return the amount to the program for user verification. I know the amount starts with a "$", and ends with two digits after the decimal point (there's only one amount per document). The search returns true like I want, but how do I actually pull the full figure from the word document (to be assigned to a variable)

我需要做的是在word文档中搜索美元金额,然后将该金额返回给程序进行用户验证。我知道金额以“$”开头,以小数点后两位数字结尾(每个文档只有一个金额)。搜索返回 true 就像我想要的那样,但是我如何实际从 word 文档中提取完整数字(分配给变量)

Code below (Excel 2010); Cheers.

下面的代码(Excel 2010);干杯。

With wdDoc.Content.Find
   .Text = "$*.??"
   .MatchWildcards = True
   If .Execute Then
      'would be verified here
   Else
      'etc
   End If
End With

Edit: I managed to get the following working;

编辑:我设法使以下工作;

   With wdApp.Selection.Find
      .Text = "$*.??"
      .Wrap = wdFindAsk
      .Forward = True
      .MatchWildcards = True
      If .Execute Then
         debug.print wdApp.Selection.Text
      Else
         debug.print "Not Found"
      End If
   End With

The only negative to this is that if the user managed to select a different word document while this search is running, it won't find the result because the selection has changed. Is there any way to specifically search ONLY the active document opened earlier in the code? ("wdDoc"). Using wdDoc.Content doesn't seem to have any way of returning the string.

唯一的负面影响是,如果用户在此搜索运行时设法选择了不同的 Word 文档,则不会找到结果,因为选择已更改。有什么方法可以专门搜索代码中较早打开的活动文档?(“wdDoc”)。使用 wdDoc.Content 似乎没有任何返回字符串的方法。

Edit2: Is there a way to return text from a search using the Word.Document as opposed to the Word.Application?

Edit2:有没有办法使用 Word.Document 而不是 Word.Application 从搜索中返回文本?

采纳答案by chris neilsen

You can use a Rangeobject to reference to found text.

您可以使用Range对象来引用找到的文本。

Also, the Find pattern $*.??could find many things in your doc, other than what you want, should a stray $exist.

此外,如果存在$*.??流浪,查找模式可以在您的文档中找到许多内容,而不是您想要的内容$

You can use this pattern to find precisely a dollar amount

您可以使用此模式精确查找美元金额

$[0-9]{1,}.[0-9]{2}


Sub Demo()
    Dim wdDoc As Document
    Dim oRng As Range

    ' Set reference to required doc
    Set wdDoc = ActiveDocument

    ' ....

    Set oRng = wdDoc.Content
    With oRng.Find
        .Text = "$[0-9]{1,}.[0-9]{2}"
        .Wrap = wdFindAsk
        .Forward = True
        .MatchWildcards = True

        If .Execute Then
           Debug.Print oRng
        Else
          'etc
        End If
    End With
End Sub

回答by Siddharth Rout

Try this

尝试这个

With ActiveDocument.Content.Find
    .Text = "$*.??"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindAsk
    .MatchWildcards = True
    If .Execute Then
        Debug.Print Selection.Range.Text
    Else
        Debug.Print "not Found"
    End If
End With

FOLLOWUP

跟进

Try this (Tried And tested)

试试这个(尝试和测试)

Sub Sample()
    ActiveDocument.Content.Select
    With Selection.Find
        .Text = "$*.??"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .MatchWildcards = True
        If .Execute Then
            Debug.Print Selection.Range.Text
        Else
            Debug.Print "not Found"
        End If
    End With
End Sub