How To Determine If A Font Exists

Going through some old code of mine, and thought I’d share a simple function that can be used to determine if a font is installed, or not.

I’ve previously published a function which employed Word Automation to do the exact same thing:

but the following approach is better IMHO and doesn’t require starting up another Office application.

Determine If A Font Exist Using Plain VBA?

We can validate the existence of a font by simply trying to assign it to the OLE Automation StdFont and then checking to see if it was accepted, or not. If it took the change, then the font exists. In the case that it didn’t change to the specified font, that indicates the font does not exist.

Thus, knowing the above concept, we can build a function, such as:

'---------------------------------------------------------------------------------------
' Procedure : Fonts_Exists
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Determine if a font is installed, or not
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
'             (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Late Binding  -> None required
'             Early Binding -> OLE Automation
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sFontName : Name of the font to validate the existence of (is not case sensitive)
'
' Usage:
' ~~~~~~
' Fonts_Exists("Calibri")
'   Returns -> True
'
' Fonts_Exists("Arial Narrow")
'   Returns -> True
'
' Fonts_Exists("MS Access Rocks")
'   Returns -> False
'
' Revision History:
' Rev       Date(yyyy-mm-dd)        Description
' **************************************************************************************
' 1         2016-01-16
' 2         2023-03-27              Public Release, Updated Header
'---------------------------------------------------------------------------------------
Public Function Fonts_Exists(ByVal sFontName As String) As Boolean
On Error GoTo Error_Handler
    #Const OLEAutomation_EarlyBind = True    'True => Early Binding / False => Late Binding
    #If OLEAutomation_EarlyBind = True Then
        Dim oFont             As stdole.StdFont    'OLE Automation

        Set oFont = New stdole.StdFont
    #Else
        Dim oFont             As Object

        Set oFont = CreateObject("StdFont")
    #End If

    oFont.Name = sFontName
    'Fonts_Exists = (oFont.Name = sFontName)
    Fonts_Exists = StrComp(sFontName, oFont.Name, vbTextCompare) = 0 'Better check
    
Error_Handler_Exit:
    On Error Resume Next
    Set oFont = Nothing
    Exit Function
 
Error_Handler:
    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
           "Error Source: Fonts_Exists" & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Description: " & Err.Description & _
           Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
           , vbOKOnly + vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Function

Then, to use it, we need simply do:

? Fonts_Exists("Arial Narrow")
'   Returns -> True

Or

If Fonts_Exists("MS Access Rocks") Then
    'The font exists, do something
Else
    'The font does not exists, do something else
End If

It’s that simple. Hopefully this will help a few of you out there.

This type of code can be useful if you setup your forms/reports to use specific fonts. This way, at the opening of your database and/or forms/reports, you can validate those font are present and either report a problem to the users, automatically install them or perhaps change the fonts used at runtime. The choice is yours.

Also note that since the OLE Automation library is typically referenced in Access, Excel, Word, … this is one of those rare cases that using Early Binding probably makes the most sense.