Word 2010 Macro for Find and Replace with a Loop

I have the following code in a Word 2010 macro.  I need to loop this to the end of the document and cannot get it to work.  I've worked with loops before, but cannot get this one to work.

    Selection.HomeKey Unit:=wdStory
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = "{  FILLIN """
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
        End With
                Selection.Find.Execute
                    Selection.Extend
        With Selection.Find
            .Text = """ \* MERGEFORMAT  }"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
        End With
                Selection.Find.Execute
                    Options.DefaultHighlightColorIndex = wdYellow
                        Selection.Range.HighlightColorIndex = wdYellow
                            Selection.MoveRight Unit:=wdCharacter, Count:=1

Open in new window



Thanks in advance!
Senniger1Asked:
Who is Participating?
 
GrahamSkanConnect With a Mentor RetiredCommented:
You could probably do that using a single wildcard Find and Replace without a macro

Find what:       \{  FILLIN *  \\\* MERGEFORMAT  \}
Options:        Use Wildcards
Replace with:
Highlight

However there are non-breaking spaces in the posted text, so an actual sample (not a picture) of of the document would be useful
0
 
GrahamSkanRetiredCommented:
It isn't clear what you are trying to do that requires a loop. There seem to be two Find and Replace operations. How do they fit together?

(I've put your code into a snippet box)
0
 
Martin LissOlder than dirtCommented:
The general process would be to first create a variable that has the same type as the object you are trying to change. Let's call that type "blah". Then...

Dim obj As blah

For each obj in blahs ' Note the 's'
    ' Your code here, changing for example With Selection.Find to With blah.Find
Next

Open in new window

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Rgonzo1971Commented:
Hi,

pls try
Sub Macro()

For Each fld In ActiveDocument.Fields
    If fld.Type = wdFieldFillIn Then
        fld.Result.HighlightColorIndex = wdYellow
    End If
Next

End Sub

Open in new window

Regards
0
 
Senniger1Author Commented:
In my code I'm looking for SEARCH1 ("{  FILLIN """), then extending the search to locate SEARCH2 (""" \* MERGEFORMAT  }").  This way from Search1 to Search2, the text is selected.  Then I highlighted this text.  Note these are no longer FillIn Fields.  This is an all text document.

I need to loop through my document and highlight all instances as shown in my example.  TheResultsWithMyCode attachment shows how my code runs.  The WantedResults attachments shows what I need the code to do after running it.

Could I please ask if you provide me the code to include my code in it.  I've done many loops, etc. before, I just don't know where to place it in this particular code.

Thanks in advance!
ResultsWithMyCode.jpg
WantedResults.jpg
0
 
Senniger1Author Commented:
I don't believe this is a simple find and replaced.  I'm trying to locate the text in between SEARCH1 and SEARCH2 and then highlight it.

Sample file is attached.
Sample.doc
0
 
GrahamSkanRetiredCommented:
In your picture , the curly brackets and everything in between is highlighted. Is that what you need?
0
 
GrahamSkanRetiredCommented:
This text in the Find box gives a result that matches your picture;
 \{  FILLIN * MERGEFORMAT  \}
0
 
Rgonzo1971Connect With a Mentor Commented:
then try
Sub RegexSimple5()
Dim regex As Object
Set regex = CreateObject("VBScript.RegExp")

    With regex
        .Global = True
        .IgnoreCase = False
        .Pattern = "({  FILLIN "")(.*?)("" \\\* MERGEFORMAT  })"
    End With
    Set Mtchs = regex.Execute(ActiveDocument.Range.Text)
    For Each Mtch In Mtchs
        intBgn = Mtch.FirstIndex + Len(Mtch.SubMatches(0))
        Set rng = ActiveDocument.Range(intBgn, intBgn + Len(Mtch.SubMatches(1)))
        rng.HighlightColorIndex = wdYellow
    Next
Set regex = Nothing
End Sub

Open in new window

REgards
0
All Courses

From novice to tech pro — start learning today.