MS Word通配符VBA宏搜索文本在.doc文件中的双卷曲内

时间:2011-09-10 20:57:51

标签: vba search ms-word wildcard

请帮助!!!

我正在尝试修复Microsoft Word的VBA宏。 宏的目的是匹配匹配双花括号{{anything}}内的任何内容,但是大括号必须在同一行中打开和关闭。 一旦我有匹配,我需要能够应用更改格式,样式,颜色等,并应用其他一些更改选定的文本。

我需要搜索的.doc有表格,绘制文本框和其他对象,这就是为什么我想使用ms字的普通搜索。

示例:

{{1,000.00}}        match (1)
{{Interesting}}     match (1)
{{Within}}          match (1)
{{1’100.00’}}       match (1)
**{{01A10}}    {{01A10}}**   match (2) twice
 {{      1    }}    match (1)
{{10-}}          {{-10}}      match (2) twice
[[1252}}            No match (0)
{{8888888.99        No match (0), because close curly braces are in a new line
}}
{{}}                match (1)
{{1’000’000.05}}    match (1)
{{                  No match (0)
}}                  No match (0)

我试过这个“[{] {2}< *> [}] {2}”但它不起作用会带来不必要的结果。 任何帮助将非常感激。

雇用是我的代码:

Sub GetTotalReport()
    Dim totalReport As Double
    Dim placeHolderRep As Variant
    Dim placeHolder As Variant

    ActiveDocument.Select

    totalReport = 0#
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "[\{]{2}<*>[\}]{2}"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
          Do While .Execute
            With Selection
              .Font.Italic = True
              .Font.Bold = True
                If Not .Text = "" Then
                  placeHolderRep = Mid(.Text, 3, Len(.Text) - 4)
                 .Text = placeHolderRep
                  placeHolder = placeHolderRep
                  placeHolder = Replace(Replace(Replace(placeHolder, ",", ""), "'", ""), "’", "")
                  totalReport = totalReport + Val(placeHolder)
                End If
            End With
        Loop
    End With
End Sub

1 个答案:

答案 0 :(得分:1)

怎么样:

Selection.WholeStory

Set re = CreateObject("vbscript.regexp")

p = "\{\{[^\{]*\}\}"
ary = Split(Selection, vbCr)

re.Global = True
re.Pattern = p

For i = 0 To UBound(ary)
    Set Matches = re.Execute(ary(i))
    Debug.Print ary(i) & ": " & Matches.Count
Next

请参阅:http://msdn.microsoft.com/en-us/library/ms974570.aspx