-
sending clipboard text to an active window
I've figured out how to send data to the clipboard. I click a command button in my VB project and it does this while exiting the program. Works like a charm. But.. I'm curious.. is there a way that I could send this text to an active window for another program that I have running.. that I'm trying to paste into.. thereby eliminating the need to use Ctrl-V?
I'm aware of the SendKeys command (SendKeys "^v"), but can't get this working when it comes to pasting outside the VB program. Am I missing something?
Thanks!
Jolene
-
Re: sending clipboard text to an active window
do you have a code in it like this
Code:
AppActivate ("somecaption")
somecaption is the caption of the window you are pasting into
-
Re: sending clipboard text to an active window
No. I don't want to specify the program I'm pasting to. I used to use a program called ClipText (have no idea if it was programmed in VB or not) that would do this. You clicked a command button.. it would store the text
-
Re: sending clipboard text to an active window
put a button to paste the clipboard into your other program.
-
Re: sending clipboard text to an active window
Hmmmm... What do you mean?
For example... You could have your program open and if someone clicks outside of it to another window it could automatically paste to it, or when you close the program it could paste the clipboard to the next program that takes the focus.
It is up to you but you need a trigger to know when you are going to send the Clipboard's content to the active Window.
-
Re: sending clipboard text to an active window
You might think so.. but check this program out: ClipText
The only trigger this has is that it's writing to the last active program you were in.
Put the .exe in a folder somewhere.. create the shortcut on your desktop so that you can assign a shortcut key to it. Now.. bring up Notepad... or any program that you can insert text into... while your cursor is in that program, hit your shortcut key.. and then hit a hotkey number for the text in Cliptext that you're copying. The program is smart enough to know the last place you were.. and automatically pastes to it. It's darn smart. ;) This is the behavior I'm trying to emulate. Any ideas now?? :)
Thanks all!
Jolene
-
Re: sending clipboard text to an active window
One more Bump before I give up... :)
-
1 Attachment(s)
Re: sending clipboard text to an active window
I think I have something similar to what you are looking for.
However, I must say that I use the VBEvent_I.dll, which was made by MerrionComputin (Duncan, if you are reading this, please update the link to your project, it took me ages to find it)... So you need to download it. I can't distribute it without his permission.
Here is the source code: (You can compile it into a dll and the add it to your project going to "Project", "References" and "Browse...", then you check it [The name is "MCL EventVB Release I"] and the project will run) Source Code For EventVB
(EventVB is used in this project to know when your application loses focus to other applications)
And here is the project:
-
Re: sending clipboard text to an active window
Wow! Thx for the post. I can't wait to try this. It will have to be this weekend, unfortunately.. but I will give it a shot.
Just when I had given up, too! :) Thx, Tec-Nico!
jk
-
Re: sending clipboard text to an active window
You are welcome. Please tell me if it is what you were expecting. :wave:
-
Re: sending clipboard text to an active window
Guess I can't make this work without the .dll, Tec. :( I wrote Duncan on Friday.. but still no word... but if I hear something I'll let you know! Thx again!
:)
Jolene
-
Re: sending clipboard text to an active window
If with a timer you kept the hwnd of the active window, would you be able to do what you want? You will be able to find the active window and the previous active window. But about the putting the text, it's tricky... Do you want to activate the previous window and paste or not? Or am I way off?
-
Re: sending clipboard text to an active window
Also, are you using the Clipboard object or APIs to access the clipboard?
-
1 Attachment(s)
Re: sending clipboard text to an active window
Did you download the project? You can build it from the project called EventVB from the hyperlink I posted before.
Download EventVB
-
Re: sending clipboard text to an active window
Manavo, I was thinking the same... But I thought it would be easier with the events provided by Duncan. In my example I am using the Clipboard object.
I will post an example without using the DLL. http://www.vbforums.com/images/ieimages/2004/12/1.gif
-
1 Attachment(s)
Re: sending clipboard text to an active window
Alright, now I upload an example without Event_I.dll.
As you can see it is a little more complex than the other one. So, if you have doubts, please let me know.
-
Re: sending clipboard text to an active window
Well just keep in mind that if you use the clipboard object, whatever you set to the object isn't kept there once the application is closed ;)
-
Re: sending clipboard text to an active window
Manavo,
I do realize that. It just seems to me that it would be as easy as telling the program to paste to the last active window BEFORE an exit command is executed (if someone knew the code to make it paste to the last active program.. it's a bit trickier than your standard pasting). Obviously, that's possible.. because it works in the program I've attached to this post. I just want a command button that pastes and then exits the program... all in one handy click.
Tec-Nico.. that actually works quite nicely! It's not automatically pasting to the last active program... but a single click to that program and it's doing that trick. That will definitely suffice. I tried to incorporate it into my command button last night. Not having luck yet.. but I'll try some more before asking for help.
Thank you!!! :)
Jolene
-
Re: sending clipboard text to an active window
Well if you want to paste to the last active window without activating it then you would have to paste using another method. If you use this : you can get the active control on the active form. Save that in the timer so when the form unloads or whenever you want you can send a message to that handle (which should be a textbox?) using SendMessage. I guess you could send the WM_PASTE message or maybe the WM_SETTEXT?
All this is theoretical by the way. Never tried all this :) I'll give it a try when I get home later though (unless you get it working). Good luck :thumb:
-
Re: sending clipboard text to an active window
Interesting.. I'd be curious to see what you come up with. The "other" program I'm trying to paste to does have a text box... although, i noticed that the attached cliptext program will even paste right to notepad if you have it up...
I just wish I knew more!! But I'm trying.. I really am! :)
Thx,
Jolene
-
1 Attachment(s)
Re: sending clipboard text to an active window
Alright, if you want to do it clicking on a button here is another example.
This example also allows to paste to the last program that had the focus when you close it. http://www.vbforums.com/images/ieimages/2004/12/1.gif
-
1 Attachment(s)
Re: sending clipboard text to an active window
And this is an "almost" API approach. It shows how to Copy and Paste using API.
Unfortunately, it wouldn't work trying to find the last active window using API (The hwnd would be 0) when using Notepad, so the last part of the code is just the crude but effective code used before.
-
Re: sending clipboard text to an active window
I like that Tec-Nico, I have an idea and you code it :D We should do it more often :) Just kidding... I just tried this with notepad and it works. Of course it's a timer and it pastes it all the time (pasted it like 10 times just when I switched windows with the interval of the timer set to 100ms)... How does this look?
VB Code:
Option Explicit
Private Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, _
ByVal idAttachTo As Long, _
ByVal fAttach As Long) _
As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetFocus Lib "user32" () As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, _
lpdwProcessId As Long) _
As Long
Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long
Private Const WM_COPY = &H301
Private Const WM_PASTE = &H302
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lparam As Any) As Long
Public Sub Paste(ByVal hwnd As Long)
SendMessage hwnd, WM_PASTE, 0&, ByVal 0&
End Sub
Private Sub Timer1_Timer()
Dim hFore As Long, hFocus As Long
hFocus = GetFocus
If hFocus = 0 Then
hFore = GetForegroundWindow()
Call AttachThreadInput(GetWindowThreadProcessId(hFore, 0&), GetCurrentThreadId, True)
hFocus = GetFocus
Call AttachThreadInput(GetWindowThreadProcessId(hFore, 0&), GetCurrentThreadId, False)
End If
Paste hFocus
Me.Caption = hFocus
End Sub
-
Re: sending clipboard text to an active window
Nice code, Manavo. But it keeps pasting over and over and I don't see where you copy the text (I guess you are just pasting the contents of the clipboard)
It looks nice, but maybe the logic should be tweaked a little to get something similar to what is being asked (And I mine too should be modified to get 100% compatibility to the example program provided)
http://www.vbforums.com/images/ieimages/2004/12/1.gif Next time just say what you want to do and I will code it in a minute.
-
Re: sending clipboard text to an active window
Well my example doesn't do exactly what was asked, however it can be changed easily (I hope) to do that.
The timer is used to just have the hwnd of the active control. The pasting shouldn't be there, it should be in the form unload where the hwnd has been stored from the timer. And the copying doesn't exist in my code but can be easily found in Tec-Nico's previous (well commented :thumb: ) zip files :)
So Tec, wanna wrap them all together? :)
-
1 Attachment(s)
Re: sending clipboard text to an active window
Ok, Manavo. I wrapped all the code and came up with a 100% API version.
I commented as much as I could. Thanks for the compliment about them http://www.vbforums.com/images/ieimages/2004/12/1.gif
Please tell me (both of you) if this is what you were expecting.
-
Re: sending clipboard text to an active window
Excellent work Tec-Nico :thumb: Just as I expected/hoped :) I just hope that this is what jolene_keagy wants also :thumb:
Once again, well done :)
-
Re: sending clipboard text to an active window
-
Re: sending clipboard text to an active window
Awesome! You guys are sooo good. It just amazes me how you can do some things.. I sincerely mean that. The behavior is exactly what I had in mind!
I must confess though.. that I can't quite get it to work with my command buttons.. Would either one of you be willing to let me send you my code so that you can assign the behavior to one of my command buttons?
Thx again! This is perfect!! :thumb: :p :)
Jolene
-
Re: sending clipboard text to an active window
Thank you. I am sure manavo is as glad as I am to know this is what you were after.
And of course you can do so. http://www.vbforums.com/images/ieimages/2004/12/1.gif
-
Re: sending clipboard text to an active window
I tried the 100% API program and it works very nice.
Unfortunately it doesn't work in Microsoft Word and Excel applications and I could not use it in Eudora (just in the message textbox). It didn't work also in the textbox I'm using now to write this message. If I press CTRL+V the text is pasted correctly?
May that depends on the fact that these boxes are RichText? Any ideas on how to fix that?
Thanks a lot.
Kurotsuke
-
Re: sending clipboard text to an active window
Hmmm... You are right... Please try the other versions, like "Clipboard using API". But you should try changing:
Code:
SendKeys "^v", 10000 'Crude and not API but works
for:
Code:
SendKeys "^v", 20000 'Crude and not API but works
http://www.vbforums.com/images/ieimages/2004/12/1.gif
-
Re: sending clipboard text to an active window
i have just been reading this thread with interest, as it is very similar to what i was doing a few days ago and have a couple of questions.
1. is there any advantage in copying to clipboard and pasting over just sending the text from text box?
2. as i don't want to close my program (it should be open all the time) can i get the hwind for the last active window when my program looses focus without using a timer.
3. what is the value in the sendkeys .... SendKeys "^v", 10000, my help only says wait as a boolean
rgds peter
-
Re: sending clipboard text to an active window
1.- The advantage I see is that when you use the API (and thus, the TextBox) the contents of the clipboard will remain there after you close your program. When you use the Clipboard object what you copied is lost.
2.- Yes, there is. Please refer to the project that uses EventVB_I.dll, it has an event that tells you when the application loses focus.
3.- When you use SendKeys, the first parameter represents the keys you want to send, in this case we are sending "control + v", the second parameter is a boolean that tells the computer to wait or not. I mistook the parameter and I thought it had to deal with mili-seconds.
-
Re: sending clipboard text to an active window
tec-nico,
thanks for the reply, sorry i wasn't clear on question one, what i meant is there any advantage to using the clipboard at all, over just sending the text using sendmessage.
i am trying to use the getactivewindow, as your example posted earlier and have posted my problem and some code in a separate thread now, as this one had been marked as solved
http://www.vbforums.com/showthread.p...=1#post1872882
with the sendkeys i was just trying to find if there were some undocumented feature
-
Re: sending clipboard text to an active window
Can you explain me the meaning of the first
hFocus = GetFocus
line? I noticed that only if it returns 0 the text is sent to the application via the clipboard.
My problem is that I want to trigger from my application the clipboard copy to another application I chose. So I set the focus to the the new application using the SetForegroundWindow API and then calling the function to paste from the clipboard.
I noticed that hFocus at the first call return a value <>0 so the paste action is never triggered?
Any hints?
Thanks.