# VBA: Automatically format specific text in Microsoft Word

G14th October 2015

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

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:

• 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