Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello Team,

Can anybody guide me about SendKeys OR PostMessage OR SendMessage to hidden application using VBA

e.g
VB
Dim xl As New Excel.Application
xl.Workbooks.Add
xl.Visible=False

xl.Sendkeys("H")
xl.Sendkeys("I")

this code should write HI in Excel Workbook.

Please help
Posted
Updated 2-Jan-12 22:32pm
v3

1 solution

You should make it first visible then send keys :

VB
Dim xl As New Excel.Application
xl.Workbooks.Add
xl.Visible=True
 
xl.Sendkeys("H")
xl.Sendkeys("I")


I tested it with CSharp and it works well.

If you insist on having excel hidden so you can not use SendKeys you should write exactly what you want in a exact cell :

C#
Worksheet ws = xl.ActiveSheet;
Range r = ws.Cells[1,1];
r.Value2 = "HI";

This code is in csharp convert it to VB please;

Hope it helps.
 
Share this answer
 
Comments
Pravinkarne.31 3-Jan-12 5:51am    
Thanks for reply but not solve my purpose...

I want to send commands to my application even it is not visible.

I have below code. But not working is Excel application is not visible. Please help

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 MyProc()

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


End Sub
Amir Mahfoozi 3-Jan-12 6:02am    
I can not understand why do you want to send key codes to an invisible window ! It's not possible not even for excel but for every other hidden windows, it is not going to work. But writing to cells even when the excel is invisible is possible.
Pravinkarne.31 3-Jan-12 8:26am    
Writting in Excel through VBA code - I know very well.
But Why I want to use in such a way that...
I have one Business Object Application. from which I want to pull the data automatically using sendkeys OR PostMessage, etc.
through visible window above code is running good. But I want to run this code even if application.visible=False

Help is very much appreciated...!!!

Thanks,
Pravin

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