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

Report issue

Captions Not Available
Settings
Full Screen
Embed
Video Quality
Auto
High (HD)
Medium
Low
Closed Caption Settings
Back
Closed Captions
On
Off
Language
English

  • No item available
Position
  • Bottom
  • Top
Type Size
A
A
A
A
A
Aa
Style Options
Captions will look like this
Back
  • Presets
  • Text
  • background
  • effects
Aa
Aa
Aa
Aa
Aa
Aa
Aa
Aa
Arial

  • Georgia
  • Palatino Linotype
  • Times New Roman
  • Arial
  • Arial Black
  • Comic Sans MS
  • Impact
  • Lucida Sans Unicode
  • Tahoma
  • Trebuchet MS
  • Verdana
  • Courier New
  • Lucida Console
75% (transparency)

  • 100% (opaque)
  • 75% (transparency)
  • 50% (transparency)
  • 25% (transparency)

Aa

Plain

Aa

Shadow

Aa

raised

Aa

Lowered

Aa

Outline
Captions will look like this

Ad:

<p class="canvas-atom canvas-text Mb(1.0em) Mb(0)–sm Mt(0.8em)–sm" type="text" content="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.” data-reactid=”15″>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.

<p class="canvas-atom canvas-text Mb(1.0em) Mb(0)–sm Mt(0.8em)–sm" type="text" content="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.” data-reactid=”16″>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.

<p class="canvas-atom canvas-text Mb(1.0em) Mb(0)–sm Mt(0.8em)–sm" type="text" content="This is the part where you would do whatever you want the macro to do. But in this case, now choose Tools -&gt; Macro -&gt; Stop Recording. You’ve recording nothing.” data-reactid=”21″>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 —

<p class="canvas-atom canvas-text Mb(1.0em) Mb(0)–sm Mt(0.8em)–sm" type="text" content="End Sub” data-reactid=”24″>End Sub

<p class="canvas-atom canvas-text Mb(1.0em) Mb(0)–sm Mt(0.8em)–sm" type="text" content="Sub ReplaceSpaces()” data-reactid=”25″>Sub ReplaceSpaces()

<p class="canvas-atom canvas-text Mb(1.0em) Mb(0)–sm Mt(0.8em)–sm" type="text" content="” data-reactid=”26″>

<p class="canvas-atom canvas-text Mb(1.0em) Mb(0)–sm Mt(0.8em)–sm" type="text" content="‘ ReplaceSpaces Macro” data-reactid=”27″>‘ ReplaceSpaces Macro

<p class="canvas-atom canvas-text Mb(1.0em) Mb(0)–sm Mt(0.8em)–sm" type="text" content="” data-reactid=”28″>

<p class="canvas-atom canvas-text Mb(1.0em) Mb(0)–sm Mt(0.8em)–sm" type="text" content="” data-reactid=”29″>

<p class="canvas-atom canvas-text Mb(1.0em) Mb(0)–sm Mt(0.8em)–sm" type="text" content="End Sub” data-reactid=”30″>End Sub

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

<p class="canvas-atom canvas-text Mb(1.0em) Mb(0)–sm Mt(0.8em)–sm" type="text" content="Sub ReplaceSpaces()” data-reactid=”32″>Sub ReplaceSpaces()

<p class="canvas-atom canvas-text Mb(1.0em) Mb(0)–sm Mt(0.8em)–sm" type="text" content="With Selection.Find” data-reactid=”33″>With Selection.Find

<p class="canvas-atom canvas-text Mb(1.0em) Mb(0)–sm Mt(0.8em)–sm" type="text" content=".ClearFormatting” data-reactid=”34″>.ClearFormatting

<p class="canvas-atom canvas-text Mb(1.0em) Mb(0)–sm Mt(0.8em)–sm" type="text" content=".Replacement.ClearFormatting” data-reactid=”35″>.Replacement.ClearFormatting

<p class="canvas-atom canvas-text Mb(1.0em) Mb(0)–sm Mt(0.8em)–sm" type="text" content=".Text = ”&nbsp; “” data-reactid=”36″>.Text = ”  “

<p class="canvas-atom canvas-text Mb(1.0em) Mb(0)–sm Mt(0.8em)–sm" type="text" content=".Replacement.Text = ” “” data-reactid=”37″>.Replacement.Text = ” “

<p class="canvas-atom canvas-text Mb(1.0em) Mb(0)–sm Mt(0.8em)–sm" type="text" content=".Forward = True” data-reactid=”38″>.Forward = True

<p class="canvas-atom canvas-text Mb(1.0em) Mb(0)–sm Mt(0.8em)–sm" type="text" content=".Wrap = wdFindContinue” data-reactid=”39″>.Wrap = wdFindContinue

<p class="canvas-atom canvas-text Mb(1.0em) Mb(0)–sm Mt(0.8em)–sm" type="text" content=".Format = False” data-reactid=”40″>.Format = False

<p class="canvas-atom canvas-text Mb(1.0em) Mb(0)–sm Mt(0.8em)–sm" type="text" content=".MatchCase = False” data-reactid=”41″>.MatchCase = False

<p class="canvas-atom canvas-text Mb(1.0em) Mb(0)–sm Mt(0.8em)–sm" type="text" content=".MatchWholeWord = False” data-reactid=”42″>.MatchWholeWord = False

<p class="canvas-atom canvas-text Mb(1.0em) Mb(0)–sm Mt(0.8em)–sm" type="text" content=".MatchWildcards = False” data-reactid=”43″>.MatchWildcards = False

<p class="canvas-atom canvas-text Mb(1.0em) Mb(0)–sm Mt(0.8em)–sm" type="text" content=".MatchSoundsLike = False” data-reactid=”44″>.MatchSoundsLike = False

<p class="canvas-atom canvas-text Mb(1.0em) Mb(0)–sm Mt(0.8em)–sm" type="text" content=".MatchAllWordForms = False” data-reactid=”45″>.MatchAllWordForms = False

<p class="canvas-atom canvas-text Mb(1.0em) Mb(0)–sm Mt(0.8em)–sm" type="text" content=".Execute Replace:=wdReplaceAll” data-reactid=”46″>.Execute Replace:=wdReplaceAll

<p class="canvas-atom canvas-text Mb(1.0em) Mb(0)–sm Mt(0.8em)–sm" type="text" content="End With” data-reactid=”47″>End With

<p class="canvas-atom canvas-text Mb(1.0em) Mb(0)–sm Mt(0.8em)–sm" type="text" content="End Sub” data-reactid=”48″>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:

<p class="canvas-atom canvas-text Mb(1.0em) Mb(0)–sm Mt(0.8em)–sm" type="text" content=".Text = “fish”” data-reactid=”50″>.Text = “fish

<p class="canvas-atom canvas-text Mb(1.0em) Mb(0)–sm Mt(0.8em)–sm" type="text" content=".Replacement.Text = “chips”” data-reactid=”51″>.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!

<p class="canvas-atom canvas-text Mb(1.0em) Mb(0)–sm Mt(0.8em)–sm" type="text" content="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 poguester@yahoo.com. You can read all his articles here, or you can sign up to get his columns by email.&nbsp;” data-reactid=”56″>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 poguester@yahoo.com. You can read all his articles here, or you can sign up to get his columns by email

Source: www.yahoo.com

Latest Posts From This Category

Leave a Comment

Your email address will not be published. Required fields are marked with *

Cancel reply