Link to home
Start Free TrialLog in
Avatar of thomaslongas
thomaslongas

asked on

PostMessage to paste clipboard

I have a visual basic control that needs to send some text to another application. I have tried to do this with SendKeys, but it is too slow and forces the window to be on top. I thought maybe the best way would be to copy the text to the clipboard and then paste it to the application using postmessage. I tried to postmessage wm_paste, but even though I saw the message using spy++, it still did not paste to the app. So, I am now trying to send Ctl-V to the application in hopes that it will paste the text. I used spy++ to see what parameters I needed to pass wm_keydown and wm_keyup by manually entering ctl-v while spying on that window's messages. Unfortunately, my code is just causing 2 v's to be typed in the app like so: "vv".

If anyone could correct my code for me or suggest another way to send text to this application, I would greatly appreciate it.

My Code:

    res = PostMessage(handle, &H100, &H11, &H1D0001)
    res = PostMessage(handle, &H100, &H56, &H2F0001)
    res = PostMessage(handle, &H101, &H56, &HC02F0001)
    res = PostMessage(handle, &H101, &H11, &HC01D0001)
Avatar of fds_fatboy
fds_fatboy

Couldn't you SendKeys the ctrl-V?
Avatar of thomaslongas

ASKER

fds_fatboy,

Actually I tried that. Believe it or not, that works in Notepad but does not work in my application. Frustrating, I know!
Raising the point value in hopes of a suitable answer.
I know this may not be the solution you are looking for, but if everything else fails, this could be an option:

When I found problems working with SendKeys, I use a little free program called AutoIt:

http://www.autoitscript.com/autoit3/downloads.php

With this tool, you can create macro scripts and compile them into exes.

Just create a script with the following line:

Send( $CmdLine[1] )

and compile it to a file called SendKeys.exe for example.

Now save this file into your windows directory and in your VB program use

Shell "SendKeys.exe " + "^V"

to send ctrl+v or any other string compatible with standard vb's SendKeys.
rettiseert ,

I like the tool. I followed your instructions but it did not work for my app. In order to use the SendKeys.exe, does my app window have to be the topmost window? If not, how do I specify for the SendKeys.exe to run on that window?
'have you tried the SendMessageByString API??
'I wrote an example below: It will send the text into notepad window..
"All you need to do is use spy++ to find your window..and change the code..


Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Private Const WM_SETTEXT = &HC
Private Sub Command1_Click()

Dim SendText As String
Dim notepad As Long, editx As Long

SendText = "Hello this will write to notepad!"

notepad = FindWindow("notepad", vbNullString)
editx = FindWindowEx(notepad, 0&, "edit", vbNullString)
Call SendMessageByString(editx, WM_SETTEXT, 0&, SendText)

End Sub

Regards,
EGL
ASKER CERTIFIED SOLUTION
Avatar of nffvrxqgrcfqvvc
nffvrxqgrcfqvvc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
egl1044 ,

I actually got it to work with postmessage character by character. Instead of sending wm_keydown, I sent wm_char and it worked. egl1044 , I haven't tried your sendmessagebystring yet, but your answer is more down the lines of the solution I was looking for. So I am assigning the points to you. Thanks everybody for your help.