【发布时间】: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是危险的,我相信您知道这一点,但我想为了您的利益而澄清这一点,以防您不这样做。