SendKeys到隐藏应用程序对象的必需帮助 [英] Required help on SendKeys To Hidden Application Object

查看:132
本文介绍了SendKeys到隐藏应用程序对象的必需帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将命令发送到我的应用程序,即使它不可见。我有下面的代码,但它不起作用,因为Excel应用程序不可见。 (我知道代码而不使用sendkeys / postmessage)但是这里excel应用程序仅作为示例。我有一个业务对象应用程序,我想用sendkeys / postmessage操作)

请帮助我。





我有以下代码,但没有解决我的问题。

I want to send commands to my application even if it is not visible. I have below code, but it is not working as the Excel application is not visible. (I Know code without using sendkeys/postmessage) But here excel application is just taken as example. I have one business object application which I want to operate using sendkeys/postmessage)
Please help me.


I have below code but not solving my problem.

Option Explicit
Declare Function FindWindowX Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, _
ByVal hWnd2 As Long, ByVal lpsz1 As Long, ByVal lpsz2 As Long) As Long
 
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
 
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
 
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Integer) As Long
 
Private Const WM_KEYDOWN = &H100
'Private Const WM_KEYUP = &H101

Sub MyProc2()
   Dim xl As New Excel.Application
   xl.Workbooks.Add
   xl.Visible = False
   Dim hWind As Variant
   Dim cWind As Variant
   hWind = xl.hwnd
   cWind = FindWindowX(hWind, 0, 0, 0)
   Debug.Print PostMessage(cWind, WM_KEYDOWN, vbKeyH, 0) 'type H
   Debug.Print PostMessage(cWind, WM_KEYDOWN, vbKeyI, 0) 'type I
   Debug.Print PostMessage(cWind, WM_KEYDOWN, 13, 0) 'Press Enter Key
   'Below to open Find & Replace window (Ctrl+H)
   Debug.Print PostMessage(cWind, WM_KEYDOWN, 17, 0) 'Press Ctrl Key
   Debug.Print PostMessage(cWind, WM_KEYDOWN, vbKeyH, 0)  'Press H Key
   Debug.Print cWind ' Everytime output is different
   Debug.Print hWind ' Everytime output is different
End Sub

推荐答案

您不能使用SendKeys。 SendKeys仅适用于可以具有输入焦点的可见窗口。一个隐藏的窗口不能有这个。



为什么你如此倾向于将键击发送到一个无法看到的Excel实例?为什么不使用Office Automation让Excel做你想做的事?
You cannot use SendKeys for this. SendKeys will only work with visible windows that can have the input focus. A hidden window cannot have that.

Why are you so bent on sending keystrokes to an Excel instance that can't be seen?? Why not just use Office Automation to have Excel do what you want?


要做到这一点,你必须使用带有WM_CHAR消息的PostMessage。



在极少数情况下(如游戏),您可能需要发送WM_KEYDOWN和WM_KEYUP。但这比WM_CHAR
To do that, you have to use PostMessage with WM_CHAR message.

In some rare cases (like games), you may have to send WM_KEYDOWN and WM_KEYUP. But that's much more work than WM_CHAR


这篇关于SendKeys到隐藏应用程序对象的必需帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆