Skip to content

Dear Internet Explorer user: Your browser is no longer supported

Please switch to a modern browser such as Microsoft Edge, Mozilla Firefox or Google Chrome to view this website's content.

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.

Instructions

Step 1

Open the Microsoft Word document that needs to be edited.

Step 2

Press Alt + F11 or otherwise go to the Developer ribbon and choose the “Visual Basic” icon if the Developer Tab is enabled.

Step 3

In the Visual Basic for Applications toolbar, go to Insert > Module

Screen capture of VBA

Inserting a module in Visual Basic for Applications.

Step 4

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

Step 5

Customise the script by deleting the sample text (.Text = "et al") and replacing it with the text string that you want italicised.

Step 6

Press the play button at the top of the window (the green triangle) to run the script. The entire document will be updated.

Other customisations

This script can be customised by changing the instructions:

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
   

Comments

2 responses to “VBA: Automatically format specific text in Microsoft Word”

On 28 April 2017, Manfred Knorr wrote: Hyperlink chain icon

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”?

Reply

On 20 September 2022, Taris wrote: Hyperlink chain icon

Thank you for this great article. I am trying to format the text only on 2nd level of lists. Thanks for you help

Reply

Have Your Say

The following HTML is permitted:
<a href="" title=""> <b> <blockquote cite=""> <code> <em> <i> <q cite=""> <strike> <strong>

Comments will be published subject to the Editorial Policy.