You can do what you need using an API function. Try this:
Put the following in your form's General section:
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
-----------------------------------------------------------
On MyForm , set the Font attribute to the same font name, size and style that you will be using in the FlexGrid on the form, then ...
Define the following function:
Public Function GetPTextWidth(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(MyForm.hDC, strTest, Len(strTest), cellSize)
With cellSize
GetPTextWidth = .cx * Screen.TwipsPerPixelX
lnHeight = .cy * Screen.TwipsPerPixelY
End With
If GetPTextWidth < 1100 Then GetPTextWidth = 1100
End Function
-----------------------------------------------------------
Put the follwing code where you need to extract the text width to set the FlexGrid column width:
Dim inContentWidth As Integer
inContentWidth = GetPTextWidth(strTemp)
MyForm!MyGrid.ColWidth(MyForm.MyGrid.Col) = inContentWidth + 150
-----------------------------------------------------------
NOTE: If you define the function GetPTextWidth as Size instead of Long, you can retrieve the text height and use this to calculate a row height if you want to spread the cell contents over multiple lines in a narrower column. You will need to calculate where the line breaks are going to occur, but that is another story ...