Creating Macros in Microsoft Word

8

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.

Microsoft Word Record New Macro dialog box

The Record New Macro dialog box, where you give your macro a name and description.

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:

  1. 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)
  2. Format > Paragraph (bring up the Paragraph dialog box)
  3. 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).
    Word macro settings

    The settings for your first Word macro

  4. Press “OK” (enters your settings)
  5. 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.

The Word Macros dialog box

The Macros dialog box lists the macros in your current document/template and offers other functions related to your macros.

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:

  1. Choose Tools > Macro > Record New Macro (access the Record Macro dialog box)
  2. Give your Macro a name and Description (remember you can’t use spaces in the name of the Macro)
  3. Press “OK” (enters your settings)
  4. 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)
  5. Choose Edit > Find > Advanced Find and Replace (brings up the Advanced Find and Replace dialog box)
  6. Switch to the “Replace” tab
  7. Enter two spaces in the “Find what:” field (enter your search criteria)
  8. Enter a single space in the “Replace with:” field (enter your replace criteria)
  9. Press the “Replace All” button
  10. Press “OK” when Word tells you the results of the search
  11. Close the Advanced Find and Replace dialog box
  12. 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.

Palettes containing Word macros

My typical workflow in Word, including Palettes that I created that store my macros (top) and Styles (bottom). Note the name of the macro (“Remove Codes”), displayed as a Tool Tip.

Jamie McKee is a book designer and typesetter for university presses throughout the US. More information about him can be found at mackeycomposition.com.
  • Anne-Marie says:

    Love this geeky Word stuff!

  • Tivi Jones says:

    Thanks, Jamie!

  • KapitiKit says:

    Thanks for this succinct and helpful intro to Word macros. Your mention of searching and replacing hyphens between any two numbers with an en dash puzzled me — I’d thought Word’s search capacities were only rudimentary, and that only InDesign could be used for such things as “any digit” type searches. But I went back to the Word Find/Replace dialog, and lo and behold! all those options are there too! So thanks for that revelation.

    • Jamie McKee says:

      KapitKit-
      Glad I was able to help. Word’s Advanced Find/Replace dialog box can do even more by utilizing the “Use wildcards” check box. With that box checked on, the options in the “Special” drop down list change and can be used for GREP-like searches.

  • I love macros, particularly in Excel. After you use the macro recorder, and if you have experience with (VBA) programming, you can drill down into the code and do some extraordinary things.

    • Jamie McKee says:

      Agreed Alex. Editing in VBA is beyond the scope of this article, but it does allow you to create some very powerful scripts.

  • Barry Diskin says:

    Not sure if this would be easier in Excel but I am currently trying this in Word. Trying to print as many labels as a user would request (between 1 and 30). Is there a way to query your location in a table, column and row, or would it be easier to have my user start on next blank label and ask them for position?

  • >