Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB VBA
Hello Team,
 
Can anybody guide me about SendKeys OR PostMessage OR SendMessage to hidden application using VBA
 
e.g
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 2-Jan-12 23:22pm
Edited 2-Jan-12 23:32pm
Mehdi Gholam250.1K
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You should make it first visible then send keys :
 
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 :
 
            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.
  Permalink  
Comments
Pravinkarne.31 at 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 at 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 at 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)

  Print Answers RSS
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,614
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 3 Jan 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100