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
Last edited by jolene_keagy; Dec 24th, 2004 at 09:41 PM.
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
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.
We miss you, friend... Rest in Peace, we will take care of the rest of it.
[vbcode]
On Error Me.Fault = False
[/vbcode]
- Silence is the human way to share ignorance
Tec-Nico
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
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:
Last edited by Tec-Nico; Dec 20th, 2004 at 01:37 PM.
We miss you, friend... Rest in Peace, we will take care of the rest of it.
[vbcode]
On Error Me.Fault = False
[/vbcode]
- Silence is the human way to share ignorance
Tec-Nico
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
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
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?
Has someone helped you? Then you can Rate their helpful post.
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
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
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
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.
Last edited by Tec-Nico; Dec 21st, 2004 at 01:56 PM.
We miss you, friend... Rest in Peace, we will take care of the rest of it.
[vbcode]
On Error Me.Fault = False
[/vbcode]
- Silence is the human way to share ignorance
Tec-Nico
I like that Tec-Nico, I have an idea and you code it 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
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)
Next time just say what you want to do and I will code it in a minute.
We miss you, friend... Rest in Peace, we will take care of the rest of it.
[vbcode]
On Error Me.Fault = False
[/vbcode]
- Silence is the human way to share ignorance
Tec-Nico
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 ) zip files
So Tec, wanna wrap them all together?
Has someone helped you? Then you can Rate their helpful post.
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?
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?
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
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.
We miss you, friend... Rest in Peace, we will take care of the rest of it.
[vbcode]
On Error Me.Fault = False
[/vbcode]
- Silence is the human way to share ignorance
Tec-Nico
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
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?