使用通配符在文档中查找string; 返回完整的stringVBA

我需要做的是search一个单词文件一美元的金额,然后返回到用户validation程序的金额。 我知道金额以“$”开头,小数点后两位数字结束(每个文档只有一个金额)。 search返回真正像我想要的,但我怎样才能真正从字文档中拉出完整的数字(分配给一个variables)

代码如下(Excel 2010); 干杯。

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

编辑:我设法得到以下工作;

  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 

唯一的负面影响是,如果用户在search运行时设法select了不同的单词文档,由于select已经改变,它将不会find结果。 有什么方法可以专门search代码中前面打开的活动文档吗? ( “wdDoc”)。 使用wdDoc.Content似乎没有任何方式返回string。

编辑2:有没有办法使用Word.Document而不是Word.Application从search中返回文本?

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

另外,查找模式$*.?? 可以在你的文档中find很多东西,除了你想要的以外,应该存在一个stream浪汉。

您可以使用此模式精确查找一个美元数额

 $[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 

尝试这个

 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 

跟进

试试这个(尝试和testing)

 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