VBA: Automatically format specific text in Microsoft Word
Use this simple Visual Basic for Applications (VBA) script to reformat any text string within Microsoft Word.
I work in science and often have to review documents with references in them. It’s not uncommon for citations to contain the text “et al.” which most journals insist should be italicised. Rather than manually check all of these, a VBA script can be employed to automate the process.
Using the following procedure, we’ll open a Microsoft Word document and scan it for instances of “et al”. The script will then italicise every instance that it finds.
Open the Microsoft Word document that needs to be edited.
Press Alt + F11 or otherwise go to the Developer ribbon and choose the “Visual Basic” icon if the Developer Tab is enabled.
In the Visual Basic for Applications toolbar, go to Insert > Module
Insert the following code into the module:
Sub ItaliciseText() Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting 'Italicises the selected text Selection.Find.Replacement.Font.Italic = True With Selection.Find 'Insert the text string that needs to be italicised .Text = "et al" .Forward = True .Wrap = wdFindContinue .MatchCase = True .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Replace:=wdReplaceAll End Sub
Customise the script by deleting the sample text (
.Text = "et al") and replacing it with the text string that you want italicised.
Press the play button at the top of the window (the green triangle) to run the script. The entire document will be updated.
This script can be customised by changing the instructions:
- To italicise:
Selection.Find.Replacement.Font.Italic = True
- To bold:
Selection.Find.Replacement.Font.Bold = True
- To underline:
Selection.Find.Replacement.Font.Underline = True
- To superscript:
Selection.Find.Replacement.Font.Superscript = True
- To subscript:
Selection.Find.Replacement.Font.Subscript = True
- To strikethrough:
Selection.Find.Replacement.Font.Strikethrough = True
- To double strikethrough:
Selection.Find.Replacement.Font.DoubleStrikethrough = True
- To shadow:
Selection.Find.Replacement.Font.Shadow = True
- To all caps:
Selection.Find.Replacement.Font.AllCaps = True
- To small caps:
Selection.Find.Replacement.Font.SmallCaps = True
- To hide:
Selection.Find.Replacement.Font.Hidden = True
Likewise, to remove a format the parameter can be switched to “false”. For instance, this will remove the bold format from the text string:
Selection.Find.Replacement.Font.Bold = False
Finally, multiple formats can be stacked in the script. For instance, to italicise and underline some text, the following code will work:
Selection.Find.Replacement.Font.Italic = True Selection.Find.Replacement.Font.Underline = True
2 responses to “VBA: Automatically format specific text in Microsoft Word”
Might there be a way to fix a database’s insistence to insert names as “Last name, first name” to the more conventional “First name last name”?
Thank you for this great article. I am trying to format the text only on 2nd level of lists. Thanks for you help