Click here to Skip to main content
15,887,313 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
VB
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
Posted
Updated 7-Jan-12 3:36am
v3
Comments
Pravinkarne.31 7-Jan-12 9:31am    
If you want to delete this post then plz don't delete this. You can delete old posts. Because no one gave correct answer to this. Please help. I am in desperate need of this code.
Thank You!!!

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?
 
Share this answer
 
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
 
Share this answer
 
Comments
CHill60 30-Jan-15 21:07pm    
A member for nearly 4 years and you decide to answer a question that is 3 years old... what gives?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900