Hi,

Is it possible to measure the width of a line of text saved into a variable.

i.e.
strData = "XXXXXX"
length = 12

strData = "iiiiii"
length = 4

Any font can be used as the data isn't going to be output on screen.

The reason:
I have a csv file holding various names and addresss, I am wanting to find the longest of each of the address fields. I currently have a program that counts the number of characters in a field but I am after the longest in size as well.

XXXX might less characters than iiiiii but it is longer.

Cheers for any help
pG :eek:

I have got one solution but it is too slow.

Set a label to auto size and copy the data into the label.
check the label.width value.

This works fine but as i said above, its too slow. The application is going to process between 250,000 and 10 million records.

pG

This is the code I am using. This speeds up my app by 50% because the resulst dont need to go to the screen/form.

Private Declare Function GetTextExtentPoint32 Lib "gdi32" Alias "GetTextExtentPoint32A" (ByVal hdc As Long, ByVal lpsz As String, ByVal cbString As Long, lpSize As POINTAPI) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long

Private Type POINTAPI
    X As Long
    Y As Long
End Type

Private Function text_length(Mytext As String) As Long
    Dim TextSize As POINTAPI
    GetTextExtentPoint32 GetWindowDC(hwnd), Mytext, Len(Mytext), TextSize

    text_length = TextSize.X
End Function

pG

This article has been dead for over six months. Start a new discussion instead.