- Thank you received: 9
Welcome to the fracta.net forum!
Share your coding ideas or ask questions.
Share your coding ideas or ask questions.
Question Excel VBA send keys to another application using VBA SendKeys command and user32.dll PostMessage
13 years 8 months ago - 1 year 4 months ago #8
by roller
Excel VBA send keys to another application using VBA SendKeys command and user32.dll PostMessage was created by roller
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
Free download of Excel file and macro from hereĀ Excel VBA send keys to another application - Free Download - It's Free! : fracta.net Edgar Badawy, We keep it simple
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
Free download of Excel file and macro from hereĀ Excel VBA send keys to another application - Free Download - It's Free! : fracta.net Edgar Badawy, We keep it simple
Last edit: 1 year 4 months ago by roller.
Please Log in or Create an account to join the conversation.
11 years 11 months ago #218
by roller
Replied by roller on topic Excel VBA send keys to another application using VBA SendKeys command and user32.dll PostMessage
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
AppActivate ("Notepad")
SendKeys "I am sending this from Excel VBA to NotePad", 1
End Sub
Please Log in or Create an account to join the conversation.
Time to create page: 0.706 seconds