• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1677
  • Last Modified:

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)
0
thomaslongas
Asked:
thomaslongas
1 Solution
 
fds_fatboyCommented:
Couldn't you SendKeys the ctrl-V?
0
 
thomaslongasAuthor Commented:
fds_fatboy,

Actually I tried that. Believe it or not, that works in Notepad but does not work in my application. Frustrating, I know!
0
 
thomaslongasAuthor Commented:
Raising the point value in hopes of a suitable answer.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
rettiseertCommented:
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.
0
 
thomaslongasAuthor Commented:
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?
0
 
nffvrxqgrcfqvvcCommented:
'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
0
 
nffvrxqgrcfqvvcCommented:
If you copying text from withing visual basic you can use the clipboard function built in vb

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

SendText = "Hello this will write to notepad!"

Clipboard.SetText (SendText)
'or
'Clipboard.SetText (Text1.Text)
notepad = FindWindow("notepad", vbNullString)
editx = FindWindowEx(notepad, 0&, "edit", vbNullString)
Call SendMessageByString(editx, WM_SETTEXT, 0&, Clipboard.GetText)
0
 
thomaslongasAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now