【问题标题】:send keys to a third party window from vba by hWnd通过 hWnd 从 vba 向第三方窗口发送密钥
【发布时间】:2018-08-16 11:14:36
【问题描述】:

我正在尝试将击键发送到第三方窗口。听起来很简单,是吗?不代表我明白了……

我在一个相当严格的系统中工作,所以我可以访问哪些数据以及可以使用哪些程序受到限制,因此我使用 VBA 而不是独立编译器。

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

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

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

我认为可以通过窗口句柄将这个窗口带到前面,然后将击键发送给它,但我有点不知道如何到达那里。

我已经进行了一些研究,并拼凑了以下代码,这些代码将为我提供 Windows 句柄,但这就是我所能做到的。尝试添加一个什么都不做的 sendkeys,但我没有想法。

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

【问题讨论】:

  • 它给你一个错误?还是根本没有结果?
  • 恐怕根本没有结果。
  • 为了使用 hwnd 库,您需要声明它,这将是 windows DLL "User32"。如:Public Declare Function SetActiveWindow Lib "User32.dll" (ByVal hWnd As Long) As Long。此外,众所周知,使用SendKeys 是危险的,我相信您知道这一点,但我想为了您的利益而澄清这一点,以防您不这样做。

标签: ms-access vba


【解决方案1】:

这是代码:在 excel(或 access)中将其粘贴到新模块中;尝试打开您必须发送密钥的窗口,运行此模块,然后按 Ctrl-G 弹出调试器;这里有本次打开的所有窗口的标题列表。在CaptionWindowsString部分的代码中找到你的title然后复制到这里;将您的发送密钥放入声明 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

【讨论】:

    猜你喜欢
    • 2011-05-29
    • 1970-01-01
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 2014-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多