Welcome, Guest
Username: Password: Remember me
Welcome to the fracta.net forum!

Share your coding ideas or ask questions.
  • Page:
  • 1

TOPIC: Excel VBA send keys to another application using VBA SendKeys command and user32.dll PostMessage

Excel VBA send keys to another application using VBA SendKeys command and user32.dll PostMessage 3 years 5 months ago #8

  • roller
  • roller's Avatar
  • OFFLINE
  • Administrator
  • Posts: 144
  • Thank you received: 18
  • Karma: 7
Here are two examples showing how to send key strokes from Excel to another application using VBA. The first example uses the VBA SendKeys command and the second uses Windows User32.dll PostMessage method.

Example 1, open NotePad and from the VBA editor run this macro:

Sub One()

AppActivate ("Notepad")
SendKeys ("I am sending this from Excel VBA to NotePad")
End Sub

Or you can uses this to launch NotePad then send the keys

Sub Two()
Shell "notepad", vbNormalFocus
SendKeys "This shoud appear in a new NotePad"
End Sub

Example 2, this example uses a more complicated method but it may be useful in some situations. It uses the User32.dll PostMessage which is similar to the operating system sending the key stroke. First you open NotePad find the window handle for NotePad then we send it the key stroke"


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

hWind = FindWindow(vbNullString, "Untitled - Notepad")
cWind = FindWindowX(hWind, 0, 0, 0)
Debug.Print PostMessage(cWind, WM_KEYDOWN, vbKeyA, 0)
Debug.Print PostMessage(cWind, WM_KEYDOWN, vbKeyB, 0)
Debug.Print PostMessage(cWind, WM_KEYDOWN, vbKeyC, 0)

End Sub
Last Edit: 3 years 5 months ago by roller.
The administrator has disabled public write access login to comment.

Excel VBA send keys to another application using VBA SendKeys command and user32.dll PostMessage 1 year 7 months ago #218

  • roller
  • roller's Avatar
  • OFFLINE
  • Administrator
  • Posts: 144
  • Thank you received: 18
  • Karma: 7
A quick update to the SendKeys method. While I was testing this with Excel 2010 and Windows 7 I noticed that it was not working when I was stepping thru the code using F8 - but it worked when a clicked play on the macro or triggered it via a button. I also found that I had to change the code to the following to make it work:

AppActivate ("Notepad")
SendKeys "I am sending this from Excel VBA to NotePad", 1
End Sub
The administrator has disabled public write access login to comment.
  • Page:
  • 1
Time to create page: 0.617 seconds
poker joomla templateeverest poker bonus
Web design by Edgar Badawy www.fracta.net