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.
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.