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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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
GrahamSkanRetiredCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Rgonzo1971Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.