Recording a Find-and-Replace Macro

Today’s article is the second in our series about recording macros. It covers basically the same procedure as last week’s newsletter, but with an emphasis on finding and replacing problem items in an electronic manuscript. Watch for a complex (and useful) example of recording a macro in next week’s article, coauthored with Dan A. Wilson, proprietor of The Editor’s DeskTop (http://www.editorsdesktop.com/).

If you’ve been editing in Microsoft Word, you’ve probably come up with a list of items you find and replace on every manuscript. These might be picky little things like replacing multiple spaces with a single space, replacing underlining with italic, or replacing the word “catalogue” with “catalog” (or vice versa). If you’re now doing such replacements manually, one after the other, you could record them all as a macro that you could run on any manuscript that needs it, saving yourself many hours over the course of a year. To do so, just follow the basic procedure outlined in last week’s newsletter:

1. Start Word’s macro recorder.

2. Do the stuff you want to record–in this case, find and replace all of the items on your list.

3. Stop the macro recorder.

(You can read last week’s newsletter here: http://www.topica.com/lists/editorium/read/message.html?mid=1706651129.)

Got your list? Let’s do it (on some junk files, of course):

1. START THE MACRO RECORDER

a. Click the “Tools” menu.

b. Click “Macro.”

c. Click “Record New Macro” (in older versions of Word, click the “Record” button).

d. Type a name for your macro (something like “ReplaceMyStuff”) in the “Macro name” box. (You can’t use spaces in a macro name.)

e. Click the “OK” button. The macro recording toolbar will appear with two buttons–the first to stop recording and the second to pause recording if you need to. That means the macro recorder is now recording what you do.

2. DO THE STUFF YOU WANT TO RECORD

Here’s where you’re going to find and replace all of those items on your list:

a. Go to the top of your document by pressing CTRL + HOME.

b. Click the “Edit” menu.

c. Click “Replace.”

d. In the “Find What” box, type the string of characters or set the formatting you want to find. You can use wildcard strings if you like, as explained in Editorium Update over the past several issues. For example, if you wanted to find multiple spaces (to be replaced by a single space), you could enter a space followed by {2,} (which means “at least two”) and then put a checkmark in the “Use Wildcards” (or “Use pattern matching”) checkbox (you might have to click the “More” button before the checkbox is visible).

e. In the “Replace With” box, type the string of characters or set the formatting you want to use to replace the contents of the “Find What” box. For example, if you wanted to replace multiple spaces with a single space, you’d enter a single space.

f. Click “Replace All.”

Now repeat those steps for each item you want to find and replace, setting the options and checkboxes in the Replace dialog as needed. (For a wonderfully useful example of transposing dates in a find-and-replace macro, see today’s Readers Write column following this article.)

As you replace your items, don’t forget that you’re recording what you do. It’s easy to get sidetracked and start editing, which would create a very strange (and potentially dangerous!) macro indeed. Resist the temptation. Just mechanically find and replace your items, one at a time. Don’t think about anything else. *Be* the computer.

If you do make a mistake, don’t panic. Just press CTRL + Z to undo the last find-and-replace operation. Your macro will record that, too, so no damage will be done, although your macro will later take longer to run because it includes that unnecessary find-and-replace operation and its reversal. Of course, you can always start over again if you really need to.

3. STOP THE MACRO RECORDER

a. Click the “Stop” button (the button with the blue square) on the macro recording toolbar to stop recording.

That’s it! You’ve recorded a macro to find and replace all those items you’ve been cleaning up manually. To play it back:

1. Click the “Tools” menu.

2. Click “Macro.”

3. In Word 97 or later, click “Macros.”

4. Click the name of your macro to select it.

5. Click the “Run” button. (If you wanted to delete your macro, you could press the “Delete” button instead.)

All of the items you recorded will be fixed automatically.

If the items you want to find and replace are fairly complicated (using wildcards, for example), you should try each one by hand and make sure it’s working correctly before recording your macro.

If you don’t have a list of items you regularly need to clean up, you might consider making one. As you edit your next project, just make a note every time you come across something you need to change more than a couple of times. Then use your list to record a find-and-replace macro that will do the work for you from now on.

Got that macro running? Sit back and sip your diet Coke. Life is sweet.

If you like the idea of cleaning up nitpicky items with a macro, you may be interested in our FileCleaner program, which you can learn about here:

http://www.editorium.com/14845.htm

If you’d like to run your macro on a bunch of documents all at once, you may be interested in our MultiMacro program, which you can learn about here:

http://www.editorium.com/14844.htm

_________________________________________

READERS WRITE

~~~~~~~~~~~~~~~~~

Transposing Dates

~~~~~~~~~~~~~~~~~

Subscriber Meg Cox wrote:

I just set up my first wildcard combination for a search and replace, and it works! I figured now was the time to take my walk on the wild side because I have a manuscript full of dates that are styled every which way. Now here’s my question: I came up with the following to change various versions of month/day/year to day/month/year–but only for January.

Find what: (Ja[.a-z]@) ([0-9]{1,2}), ([0-9]{4})

Replace with: 2 January 3

I’d like to use this for all dates. I wonder if there’s something like that [andh] combination that I can use to capture pairs of letters that begin months: Ja, Fe, Ma, Ap, and so on. But then again, maybe it won’t work because I won’t be able to specify the proper month in the “Replace with” box. This looks like the perfect opportunity to learn to do macros. Is a macro indeed the only one-click solution here?

I responded (in summary):

You’re right–there’s no easy way to do this with one click. You might consider recording a macro to do all of the combinations. Once you have it recorded, you can use it whenever you need it:

1. Click the “Tools” menu.

2. Click “Macro.”

3. Click “Record New Macro.”

4. Type a name for your macro (something like “TransposeDates”) in the “Macro name” box.

5. If you want, assign the macro to a toolbar or keyboard combination.

6. Click the “OK” button. (The macro recording toolbar will appear.)

7. Record the Replace routine for each month. In other words, run your Replace routine for January, then for February, and so on.

8. Click the “Stop” button (the button with the blue square) on the macro recording toolbar.

In other words:

1. Start recording your macro.

2. Do the search-and-replace for January.

3. Do the search-and-replace for February.

4. Do the search-and-replace for March.

5. And so on through December.

6. Stop recording your macro.

Now, if you assigned the macro to a toolbar or key combination, you can use the toolbar or key combination to run the macro in the future. Or, you can do this:

1. Click the “Tools” menu.

2. Click “Macro.”

3. Click “Macros.”

4. Click the name for your macro (say, “TransposeDates”) in the list of macros so it’s selected.

5. Click the “Run” button.

That should do the job.

At this point, you’ll have transposed all of the dates in the current document. But you’ll also have created a macro that you can run in one step on any document in the future–a very handy thing to have.

After responding to Meg, I realized that there is one possible way to do what she was asking–transpose all of the dates in one fell swoop–*if* all of the months have been spelled out. Consider this wildcard string:

([JFMASOND][a-z]@) ([0-9]{1,2}), ([0-9]{4})

[JFMASOND] represents the first letter of each month, with duplicate letters removed.

[a-z]@ represents lowercase letters following the capital letter that starts the word.

There’s a space after the name of the month.

[0-9]{1,2} represents the day of the month, whether one or two digits.

There’s a comma and a space after the day.

[0-9]{4} represents four consecutive digits–in other words, the year.

The parentheses group the items so you can transpose them using “Find What Expression” wildcards in the “Replace With” box (after turning on “Use Wildcards,” of course):

2 1 3

If some of the months have been abbreviated (Jan., Feb., Mar., etc.), you could transpose them by finding this (notice the period following the @ sign):

([JFMASOND][a-z]@.) ([0-9]{1,2}), ([0-9]{4})

and replacing with this:

2 1 3

If you’ve got a mix of abbreviated and unabbreviated dates, this string will find them all:

([JFMASOND][.a-z]@) ([0-9]{1,2}), ([0-9]{4})

and you can transpose them with this replacement string:

2 1 3

But of course, that won’t spell out the abbreviated months. To do that, you’d need to record a macro that finds and replaces each month.

If you’re going to use any of these wildcard strings, be sure to run them on some test documents to make sure they do what you need–and *don’t* do what you don’t need.

This entry was posted in Macros. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

You must be logged in to post a comment.

  • The Fine Print

    Thanks for reading Editorium Update (ISSN 1534-1283), published by:

    The EDITORIUM, LLC
    http://www.editorium.com

    Articles © on date of publication by the Editorium. All rights reserved. Editorium Update and Editorium are trademarks of the Editorium.

    You may forward copies of Editorium Update to others (but not charge for it) and print or store it for your personal use. Any other broadcast, publication, retransmission, copying, or storage, without written permission from the Editorium, is strictly prohibited. If you’re interested in reprinting one of our articles, please send an email message to editor@editorium.com

    Editorium Update is provided for informational purposes only and without a warranty of any kind, either express or implied, including but not limited to implied warranties of merchantability, fitness for a particular purpose, and freedom from infringement. The user (you) assumes the entire risk as to the accuracy and use of this document.

    The Editorium is not affiliated with Microsoft Corporation or any other entity.

    We do not sell, rent, or give our subscriber list to anyone.