Here's what I came up with:<pre>Option Explicit
Type SIZE32
cx As Long
cy As Long
End Type
Const MM_TWIPS = 6
Declare Function FindWindow32 Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function GetDC32 Lib "user32" Alias "GetDC" (ByVal hWnd As Long) As Long
Declare Function GetMapMode32 Lib "gdi32" Alias "GetMapMode" ( _
ByVal hDC As Long) As Long
Declare Function SetMapMode32 Lib "gdi32" Alias "SetMapMode" (ByVal hDC As Long, _
ByVal nMapMode As Long) As Long
Declare Function CreateFont32 Lib "gdi32" Alias "CreateFontA" (ByVal H As Long, _
ByVal W As Long, ByVal E As Long, ByVal O As Long, ByVal W As Long, _
ByVal I As Long, ByVal u As Long, ByVal S As Long, ByVal C As Long, _
ByVal OP As Long, ByVal CP As Long, ByVal Q As Long, ByVal PAF As Long, _
ByVal F As String) As Long
Declare Function SelectObject32 Lib "gdi32" Alias "SelectObject" ( _
ByVal hDC As Long, ByVal hObject As Long) As Long
Declare Function GetTextExtentPoint32 Lib "gdi32" Alias "GetTextExtentPointA" ( _
ByVal hDC As Long, ByVal lpszString As String, ByVal cbString As Long, _
lpSIZE32 As SIZE32) As Long
Declare Function DeleteObject32 Lib "gdi32" Alias "DeleteObject" ( _
ByVal hObject As Long) As Long
Declare Function ReleaseDC32 Lib "user32" Alias "ReleaseDC" (ByVal hWnd As Long, _
ByVal hDC As Long) As Long
Function TextSize(ByVal sText, sFontName As String, iFontSize As Long, _
iFontWeight As Long, Width As Boolean)
'iFontWeight = 400 not bold, 700 bold
Dim iOldMapMode As Long
Dim hOldFont As Long
Dim hWnd As Long
Dim hDC As Long
Dim hFont As Long
Dim a As Long
'Declare a variable to store the text dimensions
Dim TSize As SIZE32
hWnd = FindWindow32("XLMAIN", Application.Caption) 'Get the handle of the main Excel window, which we can use for the device context
hDC = GetDC32(hWnd) 'Get a device context for the window
iOldMapMode = GetMapMode32(hDC) 'Store the old mapping mode
a = SetMapMode32(hDC, MM_TWIPS) 'Set the new mapping mode to Twips (1/20 of a point - VBA functions use points)
hFont = CreateFont32(iFontSize * -20, 0, 0, 0, iFontWeight, 0, 0, 0, 0, 0, _
0, 0, 0, _
sFontName) 'Create a font object with the correct size, weight and style
hOldFont = SelectObject32(hDC, hFont) 'Load the font into the device context, storing the original font object
a = GetTextExtentPoint32(hDC, sText, Len(sText), TSize) 'Get the text dimensions
hFont = SelectObject32(hDC, hOldFont) 'Retrieve the original font back into the device context
a = SetMapMode32(hDC, iOldMapMode) 'Reset the original mapping mode
a = DeleteObject32(hFont) 'Delete the font object we created
a = ReleaseDC32(hWnd, hDC) 'Release the device context
If Width Then
TextSize = TSize.cx / 20 'Return the width in points (The API call returns values in twips = 1/20pt)
Else
TextSize = TSize.cy / 20
End If
End Function</pre>
To set the "actual" column width to fit the text, I had to multiply the result of this function by 0.224638. Don't ask why, but, that worked... for row height, It worked ok by itself.
I used it like this:<pre>Sub Test()
Dim Rng As Range
Dim Cll As Range
Set Rng = Range("H17:J19")
For Each Cll In Rng
If Len(Cll) Then
Cll.EntireColumn.ColumnWidth = TextSize(Cll.Value, Cll.Font.Name, _
Cll.Font.Size, IIf(Cll.Font.Bold, 700, 400), True) * 0.224638
Cll.EntireRow.RowHeight = TextSize(Cll.Value, Cll.Font.Name, _
Cll.Font.Size, IIf(Cll.Font.Bold, 700, 400), False)
End If
Next Cll
End Sub</pre>
This is a funny post, I had a nice conversation with myself ! :wink: anyway, thanks Mark and Tom for your assistance.
_________________
Regards,
Juan Pablo G.
MrExcel.com Consulting
This message was edited by Juan Pablo G. on 2002-08-24 10:13
This message was edited by Juan Pablo G. on 2002-08-24 10:15