VBA How to use FindWindowEx when have Windows Handle

北城以北 提交于 2021-02-10 14:29:42

问题


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:

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

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

回答1:


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


来源:https://stackoverflow.com/questions/58635223/vba-how-to-use-findwindowex-when-have-windows-handle

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!