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: Test if a font is installed when a Word file opens

If a required font is missing, this script will alert a user that the font needs to be installed on their local computer before a Microsoft Word file will display properly.

I utilise a number of mail merge documents that require a specific font to be installed on the local machine. In my case, Code 128 is required because my mail merge document has barcodes as part of the output. Without the Code 128 font, the barcodes will display incorrectly as a series of characters.

I wanted to alert other users that the font is required, so I compiled the following VBA script. This script needs to be inserted into “ThisDocument” in Visual Basic for Applications. Don’t forget that the Word file will need to be saved in the DOCM format and not DOCX.

Private Sub Document_Open()
 'Will run the following scripts when file opens
 FontCheck
End Sub

Sub FontCheck()
Dim lFound As Boolean
Dim font As Variant
font = "Code 128" 'Name of the font to be checked
Let lFound = False
For Each aFont In Application.FontNames
    If aFont = font Then
        Let lFound = True
    End If
Next aFont
If lFound = False Then
    Call MsgBox("Please install the font '" & font & "' before using this template.", vbExclamation, "Required font missing")
End If
End Sub

If the font is not installed on the user’s computer, a dialogue box will pop up. The user can then take corrective action.

An example dialogue box popping-up in Microsoft Word advising that a font needs to be installed.

An example dialogue box popping-up in Microsoft Word advising that a font needs to be installed.

   

Comments

No comments have yet been submitted. Be the first!

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.