'This code modified from code found at this address:-
'http://216.26.168.92/vbapi/articles/intro/part05.html
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Private CellNumber As Long
Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim slength As Long, buffer As String ' title bar text length and buffer
Dim retval As Long
Static winnum As Integer
winnum = winnum + 1 ' one more window enumerated....
slength = GetWindowTextLength(hwnd) + 1 ' get length of title bar text
If slength > 1 Then ' if return value refers to non-empty string
buffer = Space(slength) ' make room in the buffer
retval = GetWindowText(hwnd, buffer, slength) ' get title bar text
Cells(CellNumber, 1) = winnum
Cells(CellNumber, 2) = Left(buffer, slength - 1)
Debug.Print "Window #"; winnum; " : "; ' display number of enumerated window
Debug.Print Left(buffer, slength - 1) ' display title bar text of enumerated window
CellNumber = CellNumber + 1
End If
EnumWindowsProc = 1 ' return value of 1 means continue enumeration
End Function
Sub GetThem()
' *** Place this code wherever you want to enumerate the windows. ***
Dim retval As Long ' return value
CellNumber = 2
Cells(1, 1) = "Window Number"
Cells(1, 2) = "Application Name"
' Use the above callback function to list all of the enumerated windows. Note that lParam is
' set to 0 because we don't need to pass any additional information to the function.
retval = EnumWindows(AddressOf EnumWindowsProc, 0)
End Sub