Results 1 to 36 of 36

Thread: sending clipboard text to an active window

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2004
    Posts
    56

    Resolved 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
    Last edited by jolene_keagy; Dec 24th, 2004 at 09:41 PM.

  2. #2
    Hyperactive Member dandono's Avatar
    Join Date
    Aug 2004
    Location
    Cornwall, UK
    Posts
    485

    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
    If there is only one perfect person in the universe, does that make them imperfect?

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2004
    Posts
    56

    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

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: sending clipboard text to an active window

    put a button to paste the clipboard into your other program.

  5. #5
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    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.
    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

  6. #6

    Thread Starter
    Member
    Join Date
    Dec 2004
    Posts
    56

    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

  7. #7

    Thread Starter
    Member
    Join Date
    Dec 2004
    Posts
    56

    Re: sending clipboard text to an active window

    One more Bump before I give up...

  8. #8
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    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:
    Attached Files Attached Files
    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

  9. #9

    Thread Starter
    Member
    Join Date
    Dec 2004
    Posts
    56

    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

  10. #10
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Re: sending clipboard text to an active window

    You are welcome. Please tell me if it is what you were expecting.
    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

  11. #11

    Thread Starter
    Member
    Join Date
    Dec 2004
    Posts
    56

    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

  12. #12
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    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?


    Has someone helped you? Then you can Rate their helpful post.

  13. #13
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: sending clipboard text to an active window

    Also, are you using the Clipboard object or APIs to access the clipboard?


    Has someone helped you? Then you can Rate their helpful post.

  14. #14
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    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
    Attached Images Attached Images  
    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

  15. #15
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    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.
    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

  16. #16
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    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.
    Attached Files Attached Files
    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

  17. #17
    Member manavo's Avatar
    Join Date
    Feb 2002
    Posts
    33

    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
    Alter ego

  18. #18

    Thread Starter
    Member
    Join Date
    Dec 2004
    Posts
    56

    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

  19. #19
    Member manavo's Avatar
    Join Date
    Feb 2002
    Posts
    33

    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
    Alter ego

  20. #20

    Thread Starter
    Member
    Join Date
    Dec 2004
    Posts
    56

    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

  21. #21
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    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.
    Attached Files Attached Files
    Last edited by Tec-Nico; Dec 21st, 2004 at 01:02 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

  22. #22
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    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.
    Attached Files Attached Files
    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

  23. #23
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: sending clipboard text to an active window

    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:
    1. Option Explicit
    2.  
    3. Private Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, _
    4.                                                          ByVal idAttachTo As Long, _
    5.                                                          ByVal fAttach As Long) _
    6.                                                          As Long
    7. Private Declare Function GetForegroundWindow Lib "user32" () As Long
    8. Private Declare Function GetFocus Lib "user32" () As Long
    9. Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, _
    10.                                                                 lpdwProcessId As Long) _
    11.                                                                 As Long
    12. Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long
    13.  
    14. Private Const WM_COPY = &H301
    15. Private Const WM_PASTE = &H302
    16.  
    17. 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
    18.  
    19. Public Sub Paste(ByVal hwnd As Long)
    20.  SendMessage hwnd, WM_PASTE, 0&, ByVal 0&
    21. End Sub
    22.  
    23. Private Sub Timer1_Timer()
    24. Dim hFore As Long, hFocus As Long
    25.  
    26.     hFocus = GetFocus
    27.    
    28.     If hFocus = 0 Then
    29.         hFore = GetForegroundWindow()
    30.         Call AttachThreadInput(GetWindowThreadProcessId(hFore, 0&), GetCurrentThreadId, True)
    31.         hFocus = GetFocus
    32.         Call AttachThreadInput(GetWindowThreadProcessId(hFore, 0&), GetCurrentThreadId, False)
    33.     End If
    34.     Paste hFocus
    35.     Me.Caption = hFocus
    36.    
    37. End Sub


    Has someone helped you? Then you can Rate their helpful post.

  24. #24
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    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)

    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

  25. #25
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    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 ) zip files

    So Tec, wanna wrap them all together?


    Has someone helped you? Then you can Rate their helpful post.

  26. #26
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    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

    Please tell me (both of you) if this is what you were expecting.
    Attached Files Attached Files
    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

  27. #27
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: sending clipboard text to an active window

    Excellent work Tec-Nico Just as I expected/hoped I just hope that this is what jolene_keagy wants also

    Once again, well done


    Has someone helped you? Then you can Rate their helpful post.

  28. #28
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Re: sending clipboard text to an active window

    Thanks, Manavo.

    Just like you, I hope it is useful for jolene_keagy.
    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

  29. #29

    Thread Starter
    Member
    Join Date
    Dec 2004
    Posts
    56

    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!!
    Jolene

  30. #30
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    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.
    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

  31. #31
    New Member
    Join Date
    Nov 2002
    Posts
    8

    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

  32. #32
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    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

    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

  33. #33
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  34. #34
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    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.
    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

  35. #35
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  36. #36
    New Member
    Join Date
    Nov 2002
    Posts
    8

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width