# GetWindowsTextLength Function

`Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long`

GetWindowTextLength returns the length in characters of a window's title (that is, its .Caption property). This works with any window, not just the ones in your application! You can use this function in conjunction with GetWindowText to create a string just long enough to receive the title. If you do, be sure to make the string 1 character longer than the value the function returns to allow for the ending vbNullChar.

hwnd
  The handle of the window to read the length of the title.

Example:

    ' Read the number of characters in the title of Form1
    n = GetWindowTextLength(Form1.hWnd)
    ' Create a string of n+1 to receive the title (to allow for the ending vbNullChar
    buffer = Space(n + 1)
    ' Read the title
    x = GetWindowText(Form1.hWnd, buffer, n + 1)
    Debug.Print Left(buffer, x)  ' drop the vbNullChar

Related Calls: GetWindowText
Category: Windows
Back to the index.