简体   繁体   中英

MS Word wildcards VBA Macro search text inside a double curly in .doc files

Please help!!!

I have a VBA macro for Microsoft Word that I am trying to fix. The purpose of the macro is to match match anything that is inside a double curly braces {{anything}}, however braces must be open and close in the same line. Once I have the match I need to be able to apply changes format, style, color etc. and apply some other changes selected text.

The .doc where I need to search has tables, draw text box and other objects, that's why I'd like to use the normal search from ms word.

Example:

{{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)

I have tried this "[{]{2}<*>[}]{2}" but it does not work it brings unwanted result. any help will be very appreciated.

Hire is my code:

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

How about:

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

See: http://msdn.microsoft.com/en-us/library/ms974570.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM