Code To Check If Font Is Installed Problem

JohnSearcy

Board Regular
Joined
Feb 6, 2006
Messages
103
Hello, everyone -

I have this code:

Function IsFontInstalled(fontName As String) As Boolean

Dim font As Object
On Error Resume Next

Set font = CreateObject("Scripting.FileSystemObject").GetFile("C:\Windows\Fonts\" & fontName & ".ttf")
IsFontInstalled = Not font Is Nothing
On Error GoTo 0

End Function

Sub CheckFont()
Dim fontName As String

fontName = "Arial"

If IsFontInstalled(fontName) Then
MsgBox fontName & " is installed."
Else
MsgBox fontName & " is not installed."
End If

End Sub

When run it for Arial, it says it is installed. But when I put in other font names I installed, it won't recognize them. Message box will show for not installed. Reason for this? I am a novice so comments put in a simpler form would be great for me.

Thank you!
 
There is nothing wrong with your code
All the fonts are not stored in the path in your code.
Check this : C:\Users\YourName\AppData\Local\Microsoft\Windows\Fonts

Also, do an explorer search for “Fonts” on all drives on your computer. There might be fonts stored in other locations..

You can transfer all fonts found to the path per your code.
 
Upvote 0
No need to search for the font files (which btw can be there but still not installed) ... you could simply use the StdOle library which is referenced by default in vba projects.

This function should work:
VBA Code:
Function FontExists(Name As String) As Boolean
    With New stdole.StdFont
        .Name = Name
        FontExists = (StrComp(.Name, Name, vbTextCompare) = 0)
    End With
End Function

Test:
VBA Code:
Sub Test()
    Debug.Print FontExists("Tahoma")
    Debug.Print FontExists("Tahom_i")
End Sub
 
Upvote 0
Solution
Thank you both footoo and Jaafar Tribak! All my fonts are in one folder, footoo, but that would have worked. Your code worked perfectly, Jaafar, and I modified it a bit with additions and it will be imbedded in longer code for the purpose that I wanted the original code to do. Thank you both again!
 
Upvote 0

Forum statistics

Threads
1,226,771
Messages
6,192,924
Members
453,767
Latest member
922aloose

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