testing if a font is installed using VBA

Ludwig

Board Regular
Joined
Apr 7, 2003
Messages
89
Office Version
  1. 365
Platform
  1. Windows
I found the VBA code on the 'net to test if a specific font is installed or not, and it works ... most of the time.

The code is -
------------
Sub Testit()

myFont = "Free 3 of 9"
If FontIsInstalled(myFont) Then
Debug.Print "'" & myFont & "' is installed"
Else
Debug.Print "'" & myFont & "' NOT installed"
End If

End Sub


Function FontIsInstalled(sFont) As Boolean
' Returns True if sFont is installed
FontIsInstalled = False
Set FontList = Application.CommandBars("Formatting").FindControl(ID:=1728)

' If Font control is missing, create a temp CommandBar
If FontList Is Nothing Then
Set TempBar = Application.CommandBars.Add
Set FontList = TempBar.Controls.Add(ID:=1728)
End If

For i = 0 To FontList.ListCount - 1
If FontList.List(i + 1) = sFont Then
FontIsInstalled = True
On Error Resume Next
TempBar.Delete
Exit Function
End If
Next i

' Delete temp CommandBar if it exists
On Error Resume Next
TempBar.Delete

' MsgBox FontIsInstalled("Free 3 of 9")

End Function
-------------------------

However, every now and then when it's called I get the following error on the "For i = 0 To FontList.ListCount - 1" line -->

Run-time error '-2147467259 (80004005)':
Method 'ListCount' of object '_CommandBarComboBox' failed

And I get to choose End or Debug (or Help); Continue is grayed out.

What is causing this fail sometimes, and [often] not?

I'm using it in a spreadsheet that has one sheet whose print requires a barcode on the printout, so as it opens I check for the font (in workbook_open) and warn them if it's not installed.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Thanks for the quick reply! I didn't find that one in my search.

Will update my code tomorrow to match and see if it fails (... er mean 'watch it not fail' ;-} ).
 
Upvote 0
Thanks for the quick reply! I didn't find that one in my search.

Will update my code tomorrow to match and see if it fails (... er mean 'watch it not fail' ;-} ).

I tried that code ... has the same problem, especially the first time it is run (code was added, sheet saved, closed, then opened .. fail). We use Excel 2016 with Windows 10 & Windows 7, not that that matters (problem occurs for both, Win7 more prone though). I've commented out the code for now.
 
Upvote 0
I tried that code ... has the same problem, especially the first time it is run (code was added, sheet saved, closed, then opened .. fail). We use Excel 2016 with Windows 10 & Windows 7, not that that matters (problem occurs for both, Win7 more prone though). I've commented out the code for now.

Ok, only way for me seems to be to code an "on error .." to skip the code, and in the logic "assume" the font is installed. Darn!
 
Upvote 0

Forum statistics

Threads
1,214,914
Messages
6,122,211
Members
449,074
Latest member
cancansova

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top