通过hWnd从vba将密钥发送到第三方窗口

时间:2018-08-16 11:14:36

标签: access-vba

我正在尝试将击键发送到第三方窗口。听起来很简单,是吗?并不意味着我就明白了...

我正在一个相当严密的系统中工作,因此我只能访问哪些数据以及可以使用哪些程序,因此为什么我使用VBA而不是独立的编译器。

有问题的窗口标识为“ WindowsForms10.Window.8.app.0.33c0d9d”,每当用户尝试发送电子邮件(强制用户在每封电子邮件上都包含保护性标记)时,该窗口就会打开。

我实际上需要向该窗口发送一个{space}或一个{enter}(是的,为了简化起见,我在考虑sendkey),以实现完全自动化。就目前而言,用户必须在每个电子邮件报告的窗口上单击“确定”。

窗口本身在显示时没有可见的标题。

我认为可以通过窗口手柄将这个窗口放到最前面,然后向其发送击键,但是我对如何到达那里有些困惑。

我已经进行了一些研究,并整理了以下代码,这些代码将为我提供Windows句柄,但这大约是我设法获得的。尝试添加一个没有执行任何操作的sendkey,但我没有主意。

Dim DeskTophWnd As Long, WindowhWnd As Long

Dim Buff As String * 255, WindowsCaption() As String

Dim WindowsHandle() As Long

Dim lngret As Long

Dim strtext As String

ReDim WindowsCaption(0)

ReDim WindowsHandle(0)

DeskTophWnd = GetDesktopWindow

WindowhWnd = GetWindow(DeskTophWnd, 5)

Do While (WindowhWnd <> 0)

    GetWindowText WindowhWnd, Buff, 255

    strtext = String$(100, Chr$(0))

    lngret = GetClassName(hwnd, strtext, 100)

    If (Trim(Buff) <> "") And (IsWindowVisible(WindowhWnd) > False) Then

        'ShowWindowAsync WindowhWnd, 0

        ReDim Preserve WindowsCaption(UBound(WindowsCaption) + 1)

        ReDim Preserve WindowsHandle(UBound(WindowsHandle) + 1)

        WindowsCaption(UBound(WindowsCaption)) = Buff

        WindowsHandle(UBound(WindowsHandle)) = WindowhWnd

    End If

    WindowhWnd = GetWindow(WindowhWnd, 2)

    msgbox WindowhWnd

    if left(buff, 31)= "Protective Markings for Outlook" then sendkeys "{ENTER}", true


Loop

'The caption of window is in WindowsCaption()

'The handle of window is in WindowsHandle()

如果有人可以提供帮助,将不胜感激。

致谢

M

1 个答案:

答案 0 :(得分:1)

这里是代码:在excel(或访问)中将其粘贴到新模块中;尝试打开必须发送密钥的窗口,运行该模块,然后按Ctrl-G弹出调试器;这里是这次打开的所有窗口标题的列表。找到您的标题,然后将其复制到CaptionWindowsString部分的代码中;将您的sendkey放入语句sendkeys中,然后尝试。 您可以尝试使用另一个窗口(例如notepad.exe)

Option Explicit
    Declare PtrSafe Function GetDesktopWindow Lib "USER32" () As LongPtr
    Declare PtrSafe Function GetWindow Lib "USER32" (ByVal hwnd As LongPtr, ByVal wCmd As Long) As LongPtr
    Declare PtrSafe Function GetWindowText Lib "USER32" Alias "GetWindowTextA" (ByVal hwnd As LongPtr, ByVal lpString As String, ByVal cch As LongPtr) As Long

    Sub oleee()
    Dim DeskTophWnd As LongPtr
    Dim WindowhWnd As LongPtr
    Dim Buff As String * 255
    Dim strtext As String
    Dim hwnd As Long
    Dim CaptionWindowsString As String

    CaptionWindowsString = "insert here the title(or part) of the window"
    CaptionWindowsString = "Blocco"
    DeskTophWnd = GetDesktopWindow
    WindowhWnd = GetWindow(DeskTophWnd, 5)
    Do While (WindowhWnd <> 0)
        Buff = String$(255, Chr$(0))
        GetWindowText WindowhWnd, Buff, 255
        Debug.Print Buff
        strtext = String$(100, Chr$(0))
        WindowhWnd = GetWindow(WindowhWnd, 2)

        If InStr(Buff, CaptionWindowsString) <> 0 Then
            AppActivate Buff, True
            DoEvents
            SendKeys "inser here the string of keys to send", True
            DoEvents
            Exit Do
        End If
    Loop
    End Sub