如何测试是否使用VBA安装了字体?

时间:2009-07-23 08:44:10

标签: vba fonts

检查使用VBA安装特定字体的最简单方法是什么?

3 个答案:

答案 0 :(得分:6)

http://www.vbcity.com/forums/topic.asp?tid=57012
重定向到
http://vbcity.com/forums/t/55257.aspx

此vb6代码与VBA兼容:

Function FontIsInstalled(sFont As String) As Boolean
    '' This reference should already be set by default
    '' Tools > References > OLE Automation
    Dim NewFont As StdFont
    On Error Resume Next
    Set NewFont = New StdFont
    With NewFont
        ' Assign the proposed font name
        ' Will not be assigned if font doesn't exist
        .Name = sFont
        ' Return true if font assignment succeded
        FontIsInstalled = (StrComp(sFont, .Name, vbTextCompare) = 0)
        ' return actual font name through arguments
        sFont = .Name
    End With
End Function

答案 1 :(得分:2)

使用apis

EnumFonts EnumFonts函数枚举指定设备上可用的字体。对于具有指定字体名称的每种字体,EnumFonts函数检索有关该字体的信息并将其传递给应用程序定义的回调函数。此回调函数可以根据需要处理字体信息。枚举继续,直到没有更多字体或回调函数返回零。

VB4-32,5,6

Declare Function EnumFonts Lib "gdi32" Alias "EnumFontsA" (ByVal hDC As Long, ByVal lpsz As String, ByVal lpFontEnumProc As Long, ByVal lParam As Long) As Long 

EnumFontFamilies函数枚举指定设备上可用的指定字体系列中的字体。此函数取代EnumFonts函数。

VB4-32,5,6

Declare Function EnumFontFamilies Lib "gdi32" Alias "EnumFontFamiliesA" (ByVal hdc As Long, ByVal lpszFamily As String, ByVal lpEnumFontFamProc As Long, ByVal lParam As Long) As Long 

示例例程

'In a module
Public Const NTM_REGULAR = &H40&
Public Const NTM_BOLD = &H20&
Public Const NTM_ITALIC = &H1&
Public Const TMPF_FIXED_PITCH = &H1
Public Const TMPF_VECTOR = &H2
Public Const TMPF_DEVICE = &H8
Public Const TMPF_TRUETYPE = &H4
Public Const ELF_VERSION = 0
Public Const ELF_CULTURE_LATIN = 0
Public Const RASTER_FONTTYPE = &H1
Public Const DEVICE_FONTTYPE = &H2
Public Const TRUETYPE_FONTTYPE = &H4
Public Const LF_FACESIZE = 32
Public Const LF_FULLFACESIZE = 64
Type LOGFONT
   lfHeight As Long
   lfWidth As Long
   lfEscapement As Long
   lfOrientation As Long
   lfWeight As Long
   lfItalic As Byte
   lfUnderline As Byte
   lfStrikeOut As Byte
   lfCharSet As Byte
   lfOutPrecision As Byte
   lfClipPrecision As Byte
   lfQuality As Byte
   lfPitchAndFamily As Byte
   lfFaceName(LF_FACESIZE) As Byte
End Type
Type NEWTEXTMETRIC
   tmHeight As Long
   tmAscent As Long
   tmDescent As Long
   tmInternalLeading As Long
   tmExternalLeading As Long
   tmAveCharWidth As Long
   tmMaxCharWidth As Long
   tmWeight As Long
   tmOverhang As Long
   tmDigitizedAspectX As Long
   tmDigitizedAspectY As Long
   tmFirstChar As Byte
   tmLastChar As Byte
   tmDefaultChar As Byte
   tmBreakChar As Byte
   tmItalic As Byte
   tmUnderlined As Byte
   tmStruckOut As Byte
   tmPitchAndFamily As Byte
   tmCharSet As Byte
   ntmFlags As Long
   ntmSizeEM As Long
   ntmCellHeight As Long
   ntmAveWidth As Long
End Type
Declare Function EnumFontFamilies Lib "gdi32" Alias "EnumFontFamiliesA" (ByVal hDC As Long, ByVal lpszFamily As String, ByVal lpEnumFontFamProc As Long, LParam As Any) As Long
Function EnumFontFamProc(lpNLF As LOGFONT, lpNTM As NEWTEXTMETRIC, ByVal FontType As Long, LParam As Long) As Long
   Dim FaceName As String
  'convert the returned string to Unicode
   FaceName = StrConv(lpNLF.lfFaceName, vbUnicode)
  'print the form on Form1
   Form1.Print Left$(FaceName, InStr(FaceName, vbNullChar) - 1)
  'continue enumeration
   EnumFontFamProc = 1
End Function

'In a form
Private Sub Form_Load()
   'KPD-Team 2000
   'URL: http://www.allapi.net/
   'E-Mail: KPDTeam@Allapi.net
   Dim hDC As Long
   'set graphics mode to persistent
   Me.AutoRedraw = True
   'enumerates the fonts
   EnumFontFamilies Me.hDC, vbNullString, AddressOf EnumFontFamProc, ByVal 0&
End Sub 

答案 2 :(得分:1)

好的,形式正确我在发布此消息后30秒找到了解决方案。尽管在诉诸SO之前进行了10分钟的搜索....

http://j-walk.com/ss/excel/tips/tip79.htm