Creating Macros in Microsoft Word
In my article, Moving Text from Word to InDesign, I wrote about creating macros in Microsoft Word, which can run a multitude of repetitive tasks with the click of a button. As a book designer and typesetter, I spend most of my time wrangling with other people’s words, and I find this easier to do in Word than InDesign. Cleaning up other people’s text can be a tedious process, but by creating macros that run through the various Find/Replace routines and dialog boxes necessary for the clean up, you can dramatically speed up the time this takes.
Defining (Your) Macros
In Microsoft Word, a macro is a series of commands and instructions that you group together as a single command to accomplish a task automatically. Typical uses for macros include speeding up routine editing and formatting, combining multiple commands, and automating a complex series of tasks. What makes macros so powerful in Microsoft Word is that you don’t need to know any code in order to create them. You tell Word to start recording your actions, go through the actions, and then tell Word to stop recording—that’s it! In essence, you tell Word, “Watch what I’m about to do” and Word will string together the code necessary to accomplish that task.
Before you create your macros, you should identify which editing and formatting tasks you routinely run on the text you receive for your projects. For instance, some of the tasks I always deal with include: removing tab characters, setting the left and right indents of paragraphs to zero, setting the space before and after paragraphs to zero, removing double returns, removing double spaces, removing trailing spaces at the end of paragraphs, changing hyphens between inclusive numbers to en-dashes, etc. Each of these tasks can be their own macro, or combined in a single macro.
Watch Me, Word
To record your first macro, choose the “Record New Macro” command from the Tools Menu (Tools > Macro > Record New Macro). The Record Macro dialog box lets you name the macro, decide where you want the macro to be stored—either within the current Word template (so that it will be accessible to all documents opened under that template) or within just the current document—and give a description of the macro.
Let’s say you want a macro that adjusts all the margins of the document to zero, so that there are no indents and no space before or after paragraphs. Give your macro a name, keeping in mind that you cannot use spaces in the name, but that spaces will be added before each capital letter when the name of the macro is displayed as a Tool Tip. (Tool Tips appear when you move your cursor over Toolbars and any Palettes you create in Word.)
While not strictly necessary, it’s a good idea to write a brief description of all the tasks the macro accomplishes in the “Description:” field, especially if the macro handles multiple tasks. Finally, keep the default option “All Documents (Normal)” from the “Store macro in:” drop down box so that the macro is stored in the Word template, thereby making it available to all subsequent documents you open under your current template. Otherwise, the macro is only part of the current open document, which defeats the purpose of being able to use the macro on multiple documents.
When you press “OK,” the first thing you’ll notice is that your cursor has changed to a white, hollow arrow, indicating that you are recording a macro. At this point you should go through all the steps necessary to accomplish your task. For this example, that would be:
- Edit > Select All (select all the text in the document—otherwise the macro only works on the paragraph that your text cursor is currently in)
- Format > Paragraph (bring up the Paragraph dialog box)
- Set the Left and Right Indents to zero; Set “Special” to none, so that there is no First Line or Hanging Indent; Set Space Before and After to zero (adjust your settings as necessary).
- Press “OK” (enters your settings)
- Tools > Macro > Stop Recording (stops recording your macro)
You did it—you just created your first macro! To access and run your macros, select the Macros dialog box (Tools > Macro > Macros) which lists all the macros found in the active template or document.
At this point, you might be thinking, “Cool—I’m going to run my macro!” but if you do that, nothing seems to happen. The reason, of course, is because you just went through all the steps that the macro replicates. That’s why it’s important to select “All Documents (Normal)” from the “Store macro in:” drop down box so that the macro is available for other documents. When you open a different document(s), then you can run the macro and amaze all your friends.
Besides running your macros, you can step through them command-by-command, edit, delete, and organize them. If you choose to edit them, you’ll be launched into Microsoft Visual Basic, where the code of the macro will be displayed. The Organizer allows you to copy, delete, and rename your macros from one document or template to another.
Creating macros that deal with Find/Replace routines is just as easy. To create a macro that finds all double spaces and replaces them with a single space, you would do the following steps:
- Choose Tools > Macro > Record New Macro (access the Record Macro dialog box)
- Give your Macro a name and Description (remember you can’t use spaces in the name of the Macro)
- Press “OK” (enters your settings)
- Go to the beginning of your document, if you aren’t already there (if you start the Find/Replace routine in the middle of the document, you will be asked if you want to continue to search the entire document when it reaches the end—this avoids that)
- Choose Edit > Find > Advanced Find and Replace (brings up the Advanced Find and Replace dialog box)
- Switch to the “Replace” tab
- Enter two spaces in the “Find what:” field (enter your search criteria)
- Enter a single space in the “Replace with:” field (enter your replace criteria)
- Press the “Replace All” button
- Press “OK” when Word tells you the results of the search
- Close the Advanced Find and Replace dialog box
- Choose Tools > Macro > Stop Recording (stops recording your macro)
If you like, you can continue Finding and Replacing different criteria, and Word will continue to string them all together within the Macro.
Macros to Palettes
Earlier in this article, I explained how the names of macros cannot contain spaces, but that spaces are inserted before each capital letter when the name of the macro is displayed as a Tool Tip. This refers to creating palettes in word, which will be the subject of my next article.