Using a Macro to Change the Formatting of All Instances of a Word
Please Note: This article is written for users of the following Microsoft Word versions: 97, 2000, 2002, and 2003. If you are using a later version (Word 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Word, click here: Using a Macro to Change the Formatting of All Instances of a Word.
It is not uncommon to use repeating design elements in a document. For instance, you may want all occurrences of a particular word to appear in bold italics, at a certain point size. While you can certainly do the formatting by hand, it is much more efficient to allow a macro to do the work for you. By handling the formatting in this way, you don't need to worry about remembering how the word should appear. This macro, FormatWords, is an example of such a macro.
.Text = "Warning!"
.Replacement.Text = ""
.Replacement.Font.Bold = True
.Replacement.Font.Italic = True
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
When you run this macro, it searches for all occurrences of the word Warning (followed by an exclamation point) and changes the formatting on it so the word is bold and italics.
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (1762) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Using a Macro to Change the Formatting of All Instances of a Word.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
Leave your own comment:
Comments for this tip:
Yogesh 05 Apr 2016, 22:44
I want to open a word document through excel macros and wants to format opened word document using single click.plz help me
jason 26 Oct 2015, 01:57
something got corrupt on my word 2003 file.
i lost all my "heading" 1, 2,3.
Is there a way that i can change all the
Normal + Arial, (Latin) 16 pt, (Latin) Bold
priya 26 Aug 2015, 22:59
I need a macro to format the document. MY requirements are:
body text: Font size:10.5, style: Arial
Headings: Font size:11, style: Arial.
Kindly help me in this.
Paul Hanson 05 Nov 2014, 15:52
Yes, you can use this macro to replace several words. See my blog for a complete write up as posting as a comment seemed like I was writing too much. http://prhmusic.blogspot.com/2014/11/character-styles-revisited.html
Rasmus 03 Nov 2014, 07:34
Can I use this macro to make several different words in bold?
Best Regards Rasmus
Beth 25 Sep 2014, 19:59
OK. I figured it out. One needs to use .Wrap = wdFindStop instead of .Wrap = wdFindContinue
Beth 25 Sep 2014, 18:44
I have used this same macro (recorded using macro)for years, only recently switching to WORD 2013.
I usually only want to use it within selected text. Sometimes it will do this, but now it won't. It changes all instances within the document.
Do you have any insight into this?
awyatt 15 May 2014, 11:48
David: You need to do a wildcard search. Display the Replace tab of the Find and Replace dialog box (Ctrl+H), then make sure the Use Wildcards check box is selected.
Put this in the Find What box: (\<i\>)(?*)(\</i\>)
Put this in the Replace With box: \2
With the insertion point still in the Replace With box, click Format | Font and set the font to italics.
Click Replace All. All done!
David 15 May 2014, 10:31
Thanks for the help. I'm going to try to work with what you've got here to hone in on what I need. I'm working in Microsoft Word on an old .XML file for an e-book. We're using a new conversion method and I have to go through and remove all the old XML mark ups and replace them with the new ScribeNet mark up (SCML). One of the things I frequently must do is find words between <i> and </i> and italicize the words between these tags, then delete the tags.
So I understand that this macro shows how to find a certain word, but what I need is for the macro to find <i>, then italicize everything AFTER <i> until it comes to </i>. I'm sure there's a way to do it, but I don't know how. Any suggestions on where I might learn more about how to do this? I don't think CodeCademy.com offers Visual Basic.
Tanya Harvey 06 Feb 2013, 14:42
Thank you for this Allen. I recorded a script for changing several words in italicized copy back to regular. The script simply didn't work because it didn't include what change in format I wanted. I added ".Replacement.Font.Italic = False", based on your sample above, and now it works perfectly. I don't know why the recorder didn't create the correct script, but I'm happy now and learned something new.