I have to give credit to someone else on this (zemp?) But this works for me in VB6, maybe have to modify for VBA?
Public Declare Function GetTextExtentPoint32 Lib "gdi32" _
Alias "GetTextExtentPoint32A" (ByVal hDC As Long, _
ByVal lpsz As String, ByVal cbString As Long, _
lpSize As Size) As Long
Public Type Size
cx As Long
cy As Long
End Type
Public Sub FGResizeCol(frm As Object, fg As MSFlexGrid, col As Integer)
Const COLUMN_INSIDE_MARGIN = 90 'twips
Dim fieldwidth, fieldValueHighText As String, i As Integer
fieldwidth = 0
If fg.Rows > 0 Then
For i = 0 To fg.Rows - 1
If GetPTextWidth(frm, fg.TextMatrix(i, col)) > fieldwidth Then
fieldwidth = GetPTextWidth(frm, fg.TextMatrix(i, col))
fieldValueHighText = fg.TextMatrix(i, col)
End If
Next
Dim inContentWidth As Integer
inContentWidth = GetPTextWidth(frm, fieldValueHighText)
fg.ColWidth(col) = inContentWidth + 150
End If
End Sub
Public Function GetPTextWidth(frm As Object, strTest As String) As Long
'calculate length of text in twips
Dim lngRV As Long
Dim cellSize As Size
Dim lngX As Long
Dim lnHeight As Long
lngRV = GetTextExtentPoint32(frm.hDC, strTest, Len(strTest), cellSize)
With cellSize
GetPTextWidth = .cx * Screen.TwipsPerPixelX
lnHeight = .cy * Screen.TwipsPerPixelY
End With
If GetPTextWidth < 500 Then GetPTextWidth = 500
End Function
Public Function GetPTextHeight(frm As Object, strTest As String) As Long
'calculate Height of text in twips
Dim lngRV As Long
Dim cellSize As Size
Dim lngX As Long
Dim lnHeight As Long
lngRV = GetTextExtentPoint32(frm.hDC, strTest, Len(strTest), cellSize)
With cellSize
lngX = .cx * Screen.TwipsPerPixelX
GetPTextHeight = .cy * Screen.TwipsPerPixelY
End With
If GetPTextHeight < 225 Then GetPTextHeight = 225
End Function
Mark