Pogue's Basics: How to create a search-and-replace macro in Word

Pogue's Basics: How to create a search-and-replace macro in Word
Pogue's Basics: How to create a search-and-replace macro in Word

Over the years, Microsoft has evolved from a word processor into a database, Web design program, and floor wax. Among its thousands of features, macros are power-user tools that could benefit a lot of people.

They allow you to automate tasks you do often, by teaching Word to follow your example. You hit Record Macro, you do something — a search and replace, let’s say — and then you can play back that macro later.

Actually, search and replace is a bad example — Microsoft Word cannot, in fact, record and play back a search/replace. It just records nothing.

That’s a bummer. You know those people who put two spaces after a period? When one of those documents comes my way, I’d love to be able to search for two spaces and replace them with one.

Fortunately, you can create such a macro manually.

Start by choosing Tools -> Macro -> Record Macro. Name your macro “ReplaceSpaces” (or whatever you want; the macro’s name cannot contain spaces). Hit OK.

This is the part where you would do whatever you want the macro to do. But in this case, now choose Tools -> Macro -> Stop Recording. You’ve recording nothing.

Now choose Tools -> Macro -> Macros. In the dialog box, click your newly created macro’s name and then click Edit.

Now replace this empty macro —

End Sub

Sub ReplaceSpaces()

‘ ReplaceSpaces Macro

End Sub

— with this, which you can copy right from this webpage:

Sub ReplaceSpaces()

With Selection.Find

.ClearFormatting

.Replacement.ClearFormatting

.Text = ”  “

.Replacement.Text = ” “

.Forward = True

.Wrap = wdFindContinue

.Format = False

.MatchCase = False

.MatchWholeWord = False

.MatchWildcards = False

.MatchSoundsLike = False

.MatchAllWordForms = False

.Execute Replace:=wdReplaceAll

End With

End Sub

You can take this opportunity, by the way, to change this macro to any search/replace you want; it doesn’t have to replace two spaces with one. For example, to do a search for “fish” and replace every occurrence with “chips,” you’d change the lines shown here as shown:

.Text = “fish

.Replacement.Text = “chips

Close the Visual Basic Editor.

Next time you want to run this search/replace macro, choose Tools -> Macro -> Macros, and double-click the name of the one you want to run.

Or, to make things easier, you can assign your macro to a keyboard shortcut or turn it into a toolbar button.

Once again, automation takes over our jobs!

David Pogue, tech columnist for Yahoo Finance, welcomes nontoxic comments in the comments section below. On the web, he’s davidpogue.com. On Twitter, he’s @pogue. On email, he’s [email protected] You can read all his articles here, or you can sign up to get his columns by email

all right reserved for yahoo news

Get the latest news delivered to your inbox

Follow us on social media networks

PREV Technology Video medicine is a promising industry, but it's being exposed to the oldest ...
NEXT Technology Buy Microsoft before earnings on rising cloud profitability, Morgan Stanley says