I have a word document in which I need to find text and format the paragraph that the text appears in. I was clueless about the find function, so I recorded a macro to try to learn. I'm still a bit in the dark concerning how to find text within a loop, so I had to pick an arbitrarily large number (1000), which I know exceeds the number of occurances of the text. This is a fragile and inelegant solution. The code I use in my macro follows. Can anyone show me how to modify the code to exit the loop when the last text is found?
Also, I'd like to determine the number of times I found the text (as shown by my feeble use of "counter" in the code below). If you could also let me know how to use counter to that end, I would be very thankful.
Thanks...
Code:
With Selection.Find
.Text = "Key Fields"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
For i = 1 To 1000
Selection.Find.Execute
counter = counter + 1
With Selection.ParagraphFormat
.KeepTogether = True
End With
Next i