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.

Macro Recording: The Basics

Over the next few weeks Editorium Update will explain how to record macros and use them to simplify repetitive editorial tasks in Microsoft Word. I'd like to thank subscribers Meg Cox, Allene Goforth, and Dan A. Wilson for suggesting this topic. I'm especially grateful to Dan, who is an editor's editor and Microsoft Word expert, for writing one of the articles with me. (Dan is the proprietor of The Editor's DeskTop, http://www.editorsdesktop.com/.) At Dan's suggestion, I'll start with the basics, get a little fancier next week, and then show you Dan's backbreaker of an example two weeks from now, so stay tuned. If you're a member of the Freelance email list who signed up for this series of articles, welcome! I hope you'll find the newsletter valuable enough to stay with us. If you've been reading the past newsletters on searching with wildcards, you'll want to look at our Readers Write column after the feature article for some additional information.

Macros--the mysterium tremendum, the sanctum sanctorum of Microsoft Word. Or, hey, just a great way to automate those mind-numbing, finger-breaking tasks you've been doing manually for so long. Recording a macro is like recording a song from the radio, only you're recording keystrokes instead of music. Here's the basic procedure:

1. Start the macro recorder (just like starting your tape recorder).

2. Do the stuff you want to record (such as typing text and running Word features).

3. Stop the macro recorder (just like stopping your tape recorder).

Well, shoot, that's not so hard.

Now let's take a simple but real (and useful) example. As you edit, you probably transpose characters a lot--I know I do. But it takes several keystrokes to do it:

1. Select the character you want to move. (Hold down SHIFT and press the RIGHT ARROW key.)

2. Cut the character. (Hold down CTRL and press X.)

3. Move to the place you want to put the character. (Press the LEFT ARROW key.)

4. Paste the character. (Hold down CTRL and press V.)

That's seven keystrokes altogether--keystrokes you do over and over, all of the time. Let's make life easier by recording them in a macro:

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 "TransposeCharacters") in the "Macro name" box. (You can't use spaces in a macro name.) If you're using our Editor's ToolKit or Editor's ToolKit Plus program, don't call the macro "TransposeCharacters," as our programs already use that name.

e. Under "Assign macro to," click the "Keyboard" button.

f. With your cursor in the "Press new shortcut key" box, press the function key or key combination you want to use to run the macro. I like function key 12 (F12) for this macro, but you can use CTRL + T (for "Transpose") or something else. Word will show you if the key or key combination is already assigned. If it is, you can try a different one or overwrite the current assignment. It's up to you. You may want to avoid combinations using the ALT key, which works with various letters to activate menu items and dialog controls. You can, however, use ALT + CTRL, ALT + SHIFT, or SHIFT + CTRL as part of your combination.

g. Click the "Assign" button.

h. Click the "Close" 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

a. Select the character you want to move. (Hold down SHIFT and press the RIGHT ARROW key.)

b. Cut the character. (Hold down CTRL and press X.)

c. Move to the place you want to put the character. (Press the LEFT ARROW key.)

d. Paste the character. (Hold down CTRL and press V.)

Notice that this is the same procedure we used previously when we *weren't* recording a macro. In other words, we transposed the two characters just as we ordinarily would. Be careful not to use the cursor keys to move to a certain character before following this procedure. If you do, those keystrokes will become *part* of the procedure, and you'll end up recording all of those cursor movements. The idea is to record only the keystrokes you want the macro to do for you.

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. Now let's play it back:

1. Put your cursor between two characters you want to transpose.

2. Click the "Tools" menu.

3. Click "Macro."

4. In Word 97 or later, click "Macros."

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

6. Click the "Run" button. (If you wanted to delete your macro, you could press the "Delete" button instead.)

That's one way to play back your macro. But since you assigned a key combination to the macro, it's a lot easier to do it like this:

1. Put your cursor between two characters you want to transpose.

2. Press the function key or key combination you've assigned to run the macro.

Presto! The two characters will be transposed.

In a way, what you've just done is create a feature (Transpose Characters) that Microsoft Word didn't have before. Are you beginning to see the possibilities?

You can learn more about Editor's ToolKit and Editor's ToolKit Plus, which have lots of handy macros like this one, here:

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

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

_________________________________________

READERS WRITE

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

Wildcard Inconsistency

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

Allene Goforth (agoforth@Aros.Net) discovered an interesting inconsistency in how wildcards are handled from one version of Microsoft Word to another. She wrote:

I finally started working on the wildcards yesterday. Everything was progressing smoothly until I got to Gandhi. I typed in Ghandi Gahndi Ganhdi and then I tried to change those to Gandhi, using G[andh]{3}i but it can't even find the above string--let alone change them.

[Allene was referring to our newsletter of March 28, 2001, which you can read here: http://www.topica.com/lists/editorium/read/message.html?mid=1706069286.]

G[andh][andh][andh][andh]i worked as did G[andh]@i so what am I doing wrong?

I responded (after doing some testing):

The string you are using--

G[andh]{3}i

--works great in Word 95, which is the version of Word I ordinarily use.

However, as you've learned, in Word 2000 it doesn't work. Instead, you have to use this:

G[andh]{4}i

In other Words, the wildcards seem to work differently in different versions of Word.

In Word 95, {3} means "three *more* of the previous expression."

In Word 2000, {3} means "*three* of the previous expression." Or so it seems.

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

Finding ANSI Codes on a Macintosh

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

Erika Buky (erikab@socrates.Berkeley.edu) found what seems to be a bug when searching for ANSI codes on a Macintosh. She wrote:

I've been reading the newsletters on wildcards with great interest and amazing my friends with my wizardry. I've noticed one problem, however, in trying to apply your clever solutions for transposing first names and last names in lists, and so on--things that require use of a code for a paragraph break.

[Erika was referring to our newsletter of April 18, 2001, which you can read here: http://www.topica.com/lists/editorium/read/message.html?mid=1706365638.]

I'm using Word 98 on a Mac (OS 8.1), and the code ^013 for a paragraph break seems to work only sporadically. When I'm doing searches like the one above, which requires this code at the end of the second search expression--^013([A-z]@) ([A-z]@)^013--the replace operation fails, I think because the paragraph break code is not recognized. (Word selects the first name, the space, and the first 2 letters of the second name, a string which meets the search criteria if you ignore the paragraph break code.)

Codes like ^09 for tabs are also treated inconsistently; sometimes I've had better luck dropping the initial zero.

Any idea why this might be so? Any possible solutions?

I responded:

I've just done some testing, and this seems to me to be a bug in Word. In both Word 98 and 2001 (on the Mac, of course), ^013 is recognized *unless* you specify that you're using wildcards. Then Word doesn't find the paragraph mark. Try searching for ^013 (or ^13) all by itself with the "Use wildcards" checkbox turned off and then on, and you'll see what I mean.

The Word 2001 documentation specifically says to use ^13 rather than ^p when using wildcards, but when you actually try it, it doesn't work. This is a real problem for Mac users, because it means there's no way to search for paragraph returns when using wildcards--unless you use a two-step operation. For example, with "Use wildcards" turned off, you could replace ^p with

^p

(the

being an arbitrary code) and then (with "Use wildcards" turned on) searching for

([A-z]@) ([A-z]@)

.

Thanks to Allene and Erika for their questions.