If you record macros to help automate your editing, you've probably bumped into a seemingly insurmountable problem: You can get a macro to find something, and then do something, but not more than once. For example, let's say you want a macro to do this:
1. Find text formatted with the Heading 1 paragraph style.
2. Move to the next paragraph.
3. Insert these characters: "Tip. "
4. Repeat steps 1 through 3 until there aren't any more Heading 1 paragraphs to find.
You can get a macro to do steps 1 through 3, just by recording those steps. But how do you get it to do step 4 (other than running the macro 587 times)?
Well, you can't just record that part. You have to go *into* the macro and insert the commands that will make it repeat. Here's how to proceed:
IN WORD 97 AND ABOVE
1. Record the steps you want your macro to take (find something, then do something). You can learn more about recording macros here:
http://www.topica.com/lists/editorium/read/message.html?mid=1706651129
http://www.topica.com/lists/editorium/read/message.html?mid=1706748016
http://www.topica.com/lists/editorium/read/message.html?mid=1706832239
2. After you've stopped the macro recorder, click "Tools > Macro > Macros."
3. Click the macro you just recorded (you may need to scroll down the list to find it).
4. Click "Edit." The macro editor will open on your screen, showing the commands you've recorded. For example, if you recorded steps 1 through 3, above (way above: "1. Find text formatted with the Heading 1 paragraph style," and so on), here are the commands you'd see:
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Heading 1")
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveDown Unit:=wdParagraph, Count:=1
Selection.TypeText Text:="Tip. "
(Note: If your version of Microsoft Word inserts the following command as the third line in the macro--"Selection.Find.ParagraphFormat.Borders.Shadow = False"--take it out. As far as I can tell, this comes from a bug in Microsoft Word, and you don't want it in there.)
You can probably tell by reading these commands what they do. All but the last three set up the parameters for your search. The third command from the bottom executes the search. All of this constitutes the "find something" part of the macro.
The last two commands constitute the "do something" part. In this example, they move down one paragraph and type in the string of characters. Our challenge, of course, is to get these commands to repeat--and then get the Find command to repeat. And to keep repeating everything until there's nothing left to find.
So here's the secret: Just before the "do something" part of the macro, insert the following command:
Do While Selection.Find.Found
That tells Word to keep doing the "do something" part as long as ("While") Word finds the "find something" part.
Of course, you also want Word to keep doing the "find something" part, too. So, you have to include the following command at the end of the "do something" part:
Selection.Find.Execute
That tells Word to execute the Find command again--as long as something continues to be found.
Finally, to tell Word where to *stop* repeating, you have to insert this command:
Loop
When you're finished, the whole thing will look like this (except that I've added an X to show you each command we've added):
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Heading 1")
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
X Do While Selection.Find.Found
Selection.MoveDown Unit:=wdParagraph, Count:=1
Selection.TypeText Text:="Tip. "
X Selection.Find.Execute
X Loop
That's it. Click "File > Close and Return to Microsoft Word."
Now, when you run the macro, it will *keep* running until it's finished all of the paragraphs you specified.
IN WORD 6 AND 95
Here's the macro as it appears in Word 6 and 95, with an X marking each line I've added to the recorded macro:
EditFindStyle .Style = "Heading 1"
EditFind .Find = "", .Direction = 0, .MatchCase = 0, .WholeWord = 0, .PatternMatch = 0, .SoundsLike = 0, .Format = 1, .Wrap = 1, .FindAllWordForms = 0
X While EditFindFound()
ParaDown 1
Insert "Tip. "
X EditFind
X Wend
_________________________________________
RESOURCES
Jean Hollis Weber has done it again, with her new book, Taming Microsoft Word 2000. Subtitled "Hot tips and cool tricks for business and technical documents," this 120-page compendium of basic but useful knowledge will help you become an instant expert on:
* Setting up Word 2000 to work your way
* Editing and reviewing documents
* Controlling page layout
* Using templates and styles effectively
* Getting the most from fields
* Working with large or complex documents
* Working with graphics
* Creating Web pages and PDF documents from Word
I'm especially impressed with the book's crystal-clear explanations, annotated screen shots, and elegant formatting. If you've been looking for a systematic treatment on mastering Microsoft Word, look no further. You can download a copy of the book here:
http://www.jeanweber.com/books/tameword.htm
Jean's previous book on taming Microsoft Word (for Word 6, 95, and 97) is also available:
http://www.jeanweber.com/books/tamewd97.htm
If you like either of these books, please be sure to compensate Jean for her efforts. You'll find payment instructions here:
http://www.jeanweber.com/bookshop/payme.htm
Webmentions
Wesley Pignataro
Repeating Macros