Solved

Check For Font Existence

Posted on 2006-11-14
6
485 Views
Last Modified: 2013-12-03

Different versions of MS products install various different fonts.  I want my application to check for a series of fonts to see if they exist on the target machine.  As an example, I want to check the following fonts, in the following order:

Arial Narrow
Tahoma
Verdana
Arial

Then set the first one that exists for use on my reports.  Arial Narrow is not distributed with Windows, but is with other products, (Office, etc.), so I want to make sure they exist to avoid unwanted substitution.
0
Comment
Question by:grafixnetwork
  • 3
  • 2
6 Comments
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 250 total points
ID: 17942056
Use this function to determine if a font exists:

Public Function IsFontInstalled( _
      ByVal FontName As String _
   ) As Boolean
   
   Dim Index As Integer
   For Index = 0 To Screen.FontCount - 1
      If Screen.Fonts(Index) = FontName Then
         IsFontInstalled = True
         Exit Function
      End If
   Next I
   
End Function

Kevin
0
 

Author Comment

by:grafixnetwork
ID: 17942094
Thanks - anyway to do it without have to loop through all installed fonts?
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 17942193
There wouldn't be a way without getting into Windows SDK calls. Given that you will most likely not be checking for fonts that often and there are not that many fonts on any system to begin with, such a loop is really not a bad thing.

Is there a specific reason why you would not want to loop through all fonts?

Kevin
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 22

Expert Comment

by:danaseaman
ID: 17942475
Option Explicit

Public Function IsFontInstalled(ByVal sFontName As String) As Boolean
   Dim oFont As New StdFont
   On Error GoTo ErrHandler
   oFont.Name = sFontName
   If oFont.Name = sFontName Then
      IsFontInstalled = True
   End If
   Exit Function
ErrHandler:
End Function
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 17942515
You have to be careful with error handling. In compiled VB apps error handling consumes a lot of extra cycles and can be much worse than iterating through a small list without error handling.

See http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21981575.html for first hand experience.

Kevin
0
 
LVL 22

Assisted Solution

by:danaseaman
danaseaman earned 250 total points
ID: 17951232
As it turns out trying to set a Font Name to a non-existent font does not raise an error so you can remove the error handler. Also added StrComp to ignore case.

Public Function IsFontInstalled(ByVal sFontName As String) As Boolean
   Dim oFont As New StdFont
   oFont.Name = sFontName
   If StrComp(oFont.Name, sFontName, vbTextCompare) = 0 Then
      IsFontInstalled = True
   End If
End Function
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB error "Type mismatch" 2 53
Visual Basic Excel Formatting error 4 102
Looking for VB6 code to read SQL table export it to ascii 8 38
VB6 ListBox Question 4 42
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

805 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question