7

I am currently trying to find a way to check whether a window is open or not using Findwindow Function. I am able to find the window if i know the entire name of the window. In the below code i know that the name of the window is "win32api - Notepad" so i can easily find the window however i want to know whether it is possible to identify the window if i know only part name like "win32*".

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Sub runapplication()


hwnd = FindWindow(vbNullString, "win32api - Notepad")
MsgBox (hwnd)
End Sub
Community
  • 1
  • 1
RAJA THEVAR
  • 421
  • 1
  • 5
  • 19
  • 2
    Someone called? XD Check the first question I ever asked. – findwindow Dec 23 '15 at 17:38
  • Hi Findwindow can you help me with the solution – RAJA THEVAR Dec 23 '15 at 17:45
  • Maybe try `instr`? I don't know. – findwindow Dec 23 '15 at 17:49
  • just an idea - can you find a way to loop through all the windows and return their names, then use an `Instr` to check for the name you are looking for? – Scott Holtzman Dec 23 '15 at 17:59
  • 1
    What I do is I create a collection of Arrays which include the handle (hwnd) and the title for all the windows that are currently open. Then I use various methods (Instr also works) to loop through the collection and find the partial matches that way. I don't know if there is an easier way to do that. – Demetri Dec 23 '15 at 19:50

2 Answers2

7

One way you can do this is with the EnumWindows API function. Since it operates via a callback function, you'll need to cache both the criteria and the results somewhere that has scope beyond the calling function:

Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, _
                                                  ByVal param As Long) As Long
Public Declare Function IsWindowVisible Lib "User32" (ByVal hWnd As Long) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
                                                 (ByVal hwnd As Long, _
                                                  ByVal lpString As String, _
                                                  ByVal cch As Long) As Long
Public Const MAX_LEN = 260

Public results As Dictionary
Public criteria As String

Public Sub Example()
    criteria = "win32*"
    Set results = New Dictionary
    Call EnumWindows(AddressOf EnumWindowCallback, &H0)
    Dim result As Variant
    For Each result In results.Keys
        Debug.Print result & " - " & results(result)
    Next result
End Sub

Public Function EnumWindowCallback(ByVal hwnd As Long, ByVal param As Long) As Long
    Dim retValue As Long
    Dim buffer As String       
    If IsWindowVisible(hwnd) Then
        buffer = Space$(MAX_LEN)
        retValue = GetWindowText(hwnd, buffer, Len(buffer))
        If retValue Then
            If buffer Like criteria Then
                results.Add hwnd, Left$(buffer, retValue)
            End If
        End If
    End If
    EnumWindowCallback = 1
End Function
Comintern
  • 21,855
  • 5
  • 33
  • 80
2

The below code worked for me. Just Declared IsWindowVisible Function and Added Microsoft scripting runtime library to my project.

Public Declare Function EnumWindows Lib "User32" (ByVal lpEnumFunc As Long, _
                                                  ByVal param As Long) As Long
Public Declare Function GetWindowText Lib "User32" Alias "GetWindowTextA" _
                                                 (ByVal hWnd As Long, _
                                                  ByVal lpString As String, _
                                                  ByVal cch As Long) As Long
Public Declare Function IsWindowVisible Lib "User32" (ByVal hWnd As Long) As Long
Public Const MAX_LEN = 260

Public results As Dictionary
Public criteria As String

Public Sub Example()
    criteria = "win32"
    Set results = New Dictionary
    Call EnumWindows(AddressOf EnumWindowCallback, &H0)
    Dim result As Variant
    For Each result In results.Keys
        Debug.Print result & " - " & results(result)
    Next result
End Sub

Public Function EnumWindowCallback(ByVal hWnd As Long, ByVal param As Long) As Long
    Dim retValue As Long
    Dim buffer As String
    If IsWindowVisible(hWnd) Then
        buffer = Space$(MAX_LEN)
        retValue = GetWindowText(hWnd, buffer, Len(buffer))
        If retValue Then

            If InStr(1, buffer, criteria, vbTextCompare) > 0 Then
                results.Add hWnd, Left$(buffer, retValue)
            End If
        End If
    End If
    EnumWindowCallback = 1
End Function
RAJA THEVAR
  • 421
  • 1
  • 5
  • 19