Home » excel » excel – VBA How to use FindWindowEx when have Windows Handle

excel – VBA How to use FindWindowEx when have Windows Handle

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have looked over the numerous “similiar questions” that pop up when you submit a question but unfortunately none of these fit my problem, plus they are all in c++ or c#.

I found this and it has helped me get the handle:

enter image description here

My problem now is that how am I to use this handle to click “No” on this window:

enter image description here

My code below is working to retrieve the handle without error (I assume the handle output is correct), however I am unsure where to go, where to look for help on how to use the handle to click the “No” button.

Any help to point me in the right direction is much appreciated.

Option Explicit

Private Declare Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowText Lib "User32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowTextLength Lib "User32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
Private Declare Function GetWindow Lib "User32" (ByVal hWnd As Long, ByVal uCmd As Long) As Long
Private Declare Function IsWindowVisible Lib "User32" (ByVal hWnd As Long) As Boolean

Private Const GW_HWNDNEXT = 2


Private Sub GetWindowHandle()

    Dim lhWndP As Long
    If GetHandleFromPartialCaption(lhWndP, "SAP GUI for Windows 740") = True Then
        If IsWindowVisible(lhWndP) = True Then
          MsgBox "Found VISIBLE Window Handle: " & lhWndP, vbOKOnly + vbInformation
        Else
          MsgBox "Found INVISIBLE Window Handle: " & lhWndP, vbOKOnly + vbInformation
          Debug.Print lhWndP
        End If
    Else
        MsgBox "Window not found!", vbOKOnly + vbExclamation
    End If

End Sub

Private Function GetHandleFromPartialCaption(ByRef lWnd As Long, ByVal sCaption As String) As Boolean

    Dim lhWndP As Long
    Dim sStr As String
    GetHandleFromPartialCaption = False
    lhWndP = FindWindow(vbNullString, vbNullString) 'PARENT WINDOW
    Do While lhWndP <> 0
        sStr = String(GetWindowTextLength(lhWndP) + 1, Chr$(0))
        GetWindowText lhWndP, sStr, Len(sStr)
        sStr = Left$(sStr, Len(sStr) - 1)
        If InStr(1, sStr, sCaption, vbTextCompare) > 0 Then
            GetHandleFromPartialCaption = True
            lWnd = lhWndP
            Exit Do
        End If
        lhWndP = GetWindow(lhWndP, GW_HWNDNEXT)
    Loop

End Function
How to&Answers:

Thanks to @TinMan and @Remy for pointing me in the right direction. Thank you for not giving me the answer and allowing me to research..

The following code works flawlessly and will replace my previous code in it entirety.

Note this code below is much neater and more succinct.

Option Explicit

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

Private Const WM_COMMAND = &H111
Private Const IDNO = 7

Public Declare PtrSafe Function SendMessage Lib "user32.dll" Alias "SendMessageW" ( _
    ByVal hwnd As LongPtr, _
    ByVal wMsg As Long, _
    ByVal wParam As LongPtr, _
    ByVal lParam As LongPtr) As LongPtr

Sub PressNo_SAPTimeout_Wnd()

   Dim hwnd As Long
   hwnd = FindWindow(vbNullString, "SAP GUI for Windows 740")

   If (hwnd <> 0) Then
      SendMessage hwnd, WM_COMMAND, IDNO, ByVal 0&
   Else
      MsgBox "Error finding message box!", vbOKOnly
   End If

End Sub