1

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
alowflyingpig
  • 730
  • 7
  • 18
  • 1
    I would first try to use the winapi call `SetFocus` and then VBA `SendKeys "%Y"` to click the button. If this does not work you will need to use winapi `EnumChildWindows` to find the button handle and winapi `SendMessage` or `PostMessage` to click the button. – TinMan Oct 31 '19 at 00:53
  • @TinMan thxs mate. Appreciat the direction help. I will do some more research. Cheers – alowflyingpig Oct 31 '19 at 01:06
  • 1
    @alowflyingpig See [Closing a Yes/No message box via code](http://forums.codeguru.com/showthread.php?250538-Closing-a-Yes-No-message-box-via-code) – Remy Lebeau Oct 31 '19 at 01:37

1 Answers1

3

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
alowflyingpig
  • 730
  • 7
  • 18
  • @SiddharthRout Thanks mate. I love finding the answer myself and not relying on other to give it to me. Just was at a block as to where/how to look. Appreciate the acceptance of the answer (if it was yourself). – alowflyingpig Oct 31 '19 at 02:30
  • Yes I did upvote it. I love it when people self learn new things :) – Siddharth Rout Oct 31 '19 at 02:33