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