Option Explicit
Option Compare Text
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private psAppNameContains As String
Private pbFound As Boolean
Private sTitle As String
Public Function apptitlebystringpart(StringPart As String) As Boolean
Dim lRet As Long
psAppNameContains = StringPart
lRet = EnumWindows(AddressOf CheckForInstance, 0)
apptitlebystringpart = pbFound
pbFound = False
End Function
Private Function CheckForInstance(ByVal lhWnd As Long, ByVal _
lParam As Long) As Long
Dim lRet As Long
Dim iNew As Integer
If Trim(psAppNameContains = "") Then
CheckForInstance = False
Exit Function
End If
sTitle = Space(255)
lRet = GetWindowText(lhWnd, sTitle, 255)
sTitle = StripNull(sTitle)
If InStr(sTitle, psAppNameContains) > 0 Then
CheckForInstance = False
pbFound = True
Else
CheckForInstance = True
End If
End Function
Private Function StripNull(ByVal InString As String) As String
Dim iNull As Integer
If Len(InString) > 0 Then
iNull = InStr(InString, vbNullChar)
Select Case iNull
Case 0
StripNull = InString
Case 1
StripNull = ""
Case Else
StripNull = Left$(InString, iNull - 1)
End Select
End If
End Function
Sub GetClass()
Dim WinWnd As Long, sWndTitle As String, RetVal As Long, lpClassName As String
sWndTitle = InputBox("Enter at least one word from the Window Title:")
If sWndTitle<> "" Then
apptitlebystringpart (sWndTitle)
sWndTitle = sTitle
WinWnd = FindWindow(vbNullString, sWndTitle)
lpClassName = Space(256)
RetVal = GetClassName(WinWnd, lpClassName, 256)
MsgBox "Classname for Window Caption" & vbLf & vbLf & _
"[" & sWndTitle & "] = " & Left$(lpClassName, RetVal)
Else: MsgBox ("You didn't enter a term, making the task challenging...")
End If
End Sub