Sendmessage question

wsbsteven used Ask the Experts™
Ok I figured out how to use sendmessage to send text to a telnet front end I work with.  Now what I need to do is have it send the "enter" key to that application so it can transmit the data I had my program type into it.  Any ideas or can it be done?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
This is supposed to do it.

Private Const VK_RETURN = &HD
Public Const WM_KEYDOWN = &H100
Public Const WM_KEYUP = &H101

results = SendMessage(windowHandle, WM_KEYDOWN, VK_RETURN, 0)


Here is my code so far.

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, wParam As Any, ByVal lParam As String) As Long
Private Const WM_SETTEXT = &HC

Private Sub Command1_Click()

Dim RC As Long
Dim WndH As Long
Dim x As String
x = "" & Chr(0)
WndH = 787160
RC = SendMessage(WndH, WM_SETTEXT, 0&, x)

Tell me if I did something wrong.  I put the Private Const VK_RETURN = &HD
Public Const WM_KEYDOWN = &H100
Public Const WM_KEYUP = &H101 into a module since I can't add public const to the form.  I then added the line results = SendMessage(windowHandle, WM_KEYDOWN, VK_RETURN, 0) changing windowhandle to wndh and results to RC.  It still did not hit enter on the application but it still types in the text  Any more ideas?
You did everything just right.

I used the wording "supposed to do it" because I was working the same problem myself and not having any more luck than you.  I finally got this to work by using

the SetFocus API or SetForeGroundWindow API
and sendkeys "{enter}"

Declare Function SetFocus Lib "user32" (ByVal hwnd As Long) As Long

Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

results = SetForegroundWindow(windowHandle) or
results = SetFocus(windowHandle)

results returns the handle to the window which previously had the focus - so a zero would indicate a failure.

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!


Won't the sendkeys usable make me unable to use the machine while it is sending those keys?  I want to be able to work on other programs while that runs in the background.


Ok it seems to work but it keeps switching focus to that application.  I will give double the points if I can get a solution that doesn't use a sendkeys statement because it removes my focus from the current application I'm working on to the program that is recieving data.  If no solution is found by Saturday I'll accept your current answer for 50.
Just save the handle to the previous window and use the setfocus api to go back to it when the sendkeys is finished.  
   Sorry I did not get back to you sooner. I was tied up this weekend.  Hope this works for you!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial