Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default "embedding" fonts in Excel

Hey there everyone,

I do not know what I missed, but I must have missed something...
The code (below) runs without errors in Excel 2003 on Vista and XP. It
does copy the font into the font folder, but it is not registering
right.

At the CMD I can DIR the fonts folder and see that the file is there,
but if I look using explorer it is not visible (not font name or file
name).

For this upload I changed the location of font file to "C:\temp\". The
actual location of the file is on a network server and is accessed via
UNC.

When this codes works as intended it would copy the font, register the
font, and notify all open applications, including Excel, about the
font.

As far as I can tell, this is about as close as you can get to
embedding a font in Excel, unless you have 2007, which I do not.

Finally, the real work being done here is, with only a few small
changes, taken from code I found online. I had to change things like
"user" to "user32" and add aliases to make the errors stop. Without
the "32" it could not find the file, and without the alias it would
say it could not find an entry point.

Thank you, in advance.
Mystif


Private colFoundFiles As New Collection
Private strPath As String

Private Declare Function CreateScalableFontResource Lib "gdi32" _
Alias "CreateScalableFontResourceA" (ByVal fHidden As Long, _
ByVal lpszResourceFile As String, ByVal lpszFontFile As String, _
ByVal lpszCurrentPath As String) As Long

Private Declare Function AddFontResource Lib "gdi32" Alias _
"AddFontResourceA" (ByVal lpFileName As String) As Long

Private Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long

Private Declare Function WriteProfileString Lib "Kernel32" Alias _
"WriteProfileStringA" (ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal lpString As String) As Integer

Private Sub Workbook_Open()
IsFontInstalled
End Sub

Sub IsFontInstalled()
Dim lngFileCount As Long

strPath = Environ("SystemRoot") & "\Fonts"

With Application.FileSearch
.NewSearch
.LookIn = strPath
.Filename = "astronbv.ttf"
.SearchSubFolders = False
.FileType = msoFileTypeAllFiles
If .Execute(SortBy:=msoSortByFileName, _
SortOrder:=msoSortOrderAscending) = 0 Then
FileCopy "C:\Temp\astronbv.ttf", strPath & "\astronbv.ttf"
Install_TTF "Astron Boy Video", "astronbv.ttf", _
Environ("SystemRoot") & "\System32"
Else
Exit Sub
End If
End With
End Sub

' This sub installs a TrueType font and makes it available to
' all Windows apps. It takes these arguments:
'
' FontName$ is the font's name (e.g. "Goudy Old Style")
'
' FontFileName$ is the font's filename (e.g. "GOUDOS.TTF")
'
' WinSysDir$ is the user's System folder (e.g.
' "C:\WINDOWS\SYSTEM" or "C:\WINDOWS\SYSTEM32")
'
' ** Before calling this sub, your code must copy the font file
' to the user's Fonts folder. **
'
Sub Install_TTF(FontName$, FontFileName$, WinSysDir$)
Dim Ret%, Res&, FontPath$, FontRes$
Const WM_FONTCHANGE = &H1D
Const HWND_BROADCAST = &HFFFF

FontPath$ = Environ("SystemRoot") & "\Fonts\" & FontFileName$
FontRes$ = WinSysDir$ & "\" & Left$(FontFileName$, _
Len(FontFileName$) - 3) & "FOT"

Ret% = CreateScalableFontResource(0, FontRes$, _
FontFileName$, WinSysDir$)
Ret% = AddFontResource(FontRes$)
Res& = SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0)
Ret% = WriteProfileString("fonts", FontName + " " & _
"(TrueType)", FontRes$)
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 32
Default "embedding" fonts in Excel

Did you find the answer.
I am struggling with a similar problem.

Mystif skrev:

Hey there everyone,

I do not know what I missed, but I must have missed something...
The code (below) runs without errors in Excel 2003 on Vista and XP. It
does copy the font into the font folder, but it is not registering
right.

At the CMD I can DIR the fonts folder and see that the file is there,
but if I look using explorer it is not visible (not font name or file
name).

For this upload I changed the location of font file to "C:\temp\". The
actual location of the file is on a network server and is accessed via
UNC.

When this codes works as intended it would copy the font, register the
font, and notify all open applications, including Excel, about the
font.

As far as I can tell, this is about as close as you can get to
embedding a font in Excel, unless you have 2007, which I do not.

Finally, the real work being done here is, with only a few small
changes, taken from code I found online. I had to change things like
"user" to "user32" and add aliases to make the errors stop. Without
the "32" it could not find the file, and without the alias it would
say it could not find an entry point.

Thank you, in advance.
Mystif


Private colFoundFiles As New Collection
Private strPath As String

Private Declare Function CreateScalableFontResource Lib "gdi32" _
Alias "CreateScalableFontResourceA" (ByVal fHidden As Long, _
ByVal lpszResourceFile As String, ByVal lpszFontFile As String, _
ByVal lpszCurrentPath As String) As Long

Private Declare Function AddFontResource Lib "gdi32" Alias _
"AddFontResourceA" (ByVal lpFileName As String) As Long

Private Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long

Private Declare Function WriteProfileString Lib "Kernel32" Alias _
"WriteProfileStringA" (ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal lpString As String) As Integer

Private Sub Workbook_Open()
IsFontInstalled
End Sub

Sub IsFontInstalled()
Dim lngFileCount As Long

strPath = Environ("SystemRoot") & "\Fonts"

With Application.FileSearch
.NewSearch
.LookIn = strPath
.Filename = "astronbv.ttf"
.SearchSubFolders = False
.FileType = msoFileTypeAllFiles
If .Execute(SortBy:=msoSortByFileName, _
SortOrder:=msoSortOrderAscending) = 0 Then
FileCopy "C:\Temp\astronbv.ttf", strPath & "\astronbv.ttf"
Install_TTF "Astron Boy Video", "astronbv.ttf", _
Environ("SystemRoot") & "\System32"
Else
Exit Sub
End If
End With
End Sub

' This sub installs a TrueType font and makes it available to
' all Windows apps. It takes these arguments:
'
' FontName$ is the font's name (e.g. "Goudy Old Style")
'
' FontFileName$ is the font's filename (e.g. "GOUDOS.TTF")
'
' WinSysDir$ is the user's System folder (e.g.
' "C:\WINDOWS\SYSTEM" or "C:\WINDOWS\SYSTEM32")
'
' ** Before calling this sub, your code must copy the font file
' to the user's Fonts folder. **
'
Sub Install_TTF(FontName$, FontFileName$, WinSysDir$)
Dim Ret%, Res&, FontPath$, FontRes$
Const WM_FONTCHANGE = &H1D
Const HWND_BROADCAST = &HFFFF

FontPath$ = Environ("SystemRoot") & "\Fonts\" & FontFileName$
FontRes$ = WinSysDir$ & "\" & Left$(FontFileName$, _
Len(FontFileName$) - 3) & "FOT"

Ret% = CreateScalableFontResource(0, FontRes$, _
FontFileName$, WinSysDir$)
Ret% = AddFontResource(FontRes$)
Res& = SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0)
Ret% = WriteProfileString("fonts", FontName + " " & _
"(TrueType)", FontRes$)
End Sub

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Cannot use object linking and embedding" - a solution! Nate[_4_] Excel Discussion (Misc queries) 5 June 3rd 14 01:51 PM
Excel - Golf - how to display "-2" as "2 Under" or "4"as "+4" or "4 Over" in a calculation cell Steve Kay Excel Discussion (Misc queries) 2 August 8th 08 01:54 AM
embedding "ISERROR" function into an "IF" statement [email protected] Excel Worksheet Functions 8 January 4th 07 12:01 AM
What does "No more new fonts may be applied" mean in Excel? Letty Excel Worksheet Functions 1 August 14th 06 03:36 AM
=IF(D13="PAID","YES","NO") Can I change fonts colour Kev Excel Discussion (Misc queries) 3 February 17th 06 04:27 AM


All times are GMT +1. The time now is 11:10 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"