Option Explicit
' These are API calls to get a list of active windows
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare PtrSafe Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Private Declare PtrSafe Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare PtrSafe Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Boolean
Private Const GW_HWNDNEXT = 2
Sub Testit()
Dim lhWndP As Long, AppName As String
AppName = GetNameFromPartialCaption(lhWndP, "Category Review Links")
If AppName <> "" Then
AppActivate (AppName)
End If
End Sub
Private Function GetNameFromPartialCaption(ByRef lWnd As Long, ByVal sCaption As String) As String
Dim lhWndP As Long
Dim sStr As String
GetNameFromPartialCaption = "" ' Default is empty
lhWndP = FindWindow(vbNullString, vbNullString) ' Get the Parent Window handle
Do While lhWndP <> 0 ' While there are more handles . . .
sStr = String(GetWindowTextLength(lhWndP) + 1, Chr$(0)) ' Get the window name for that handle
GetWindowText lhWndP, sStr, Len(sStr) ' Get the window name for that handle
sStr = Left$(sStr, Len(sStr) - 1) ' Drop last character
If InStr(sStr, sCaption) > 0 Then ' Does it contain our string?
GetNameFromPartialCaption = sStr ' Yes? then save the name
lWnd = lhWndP ' save the handle
Exit Do
End If
lhWndP = GetWindow(lhWndP, GW_HWNDNEXT) ' No? then get the next window
Loop
End Function