Results 1 to 6 of 6

Thread: Paste text to any app with a text field?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    3

    Paste text to any app with a text field?

    Is it possible to make a program with VB that when a button is pressed it will put text into any program that is open below the VB program?

    Sorry, very first time trying to make a program, and couldn't find any info on this searching.

    Lets say I have a program with 2 buttons, one labeled "hello" and one "world"... is it possible to press the buttons and have it type "hello world" in whatever is open below the VB program (Outlook, Word, or a billing systems notes)?

  2. #2
    Addicted Member
    Join Date
    Jul 2007
    Posts
    228

    Re: Paste text to any app with a text field?

    Kind of... but this is not a trivial task. If you can get the information you wish to paste into the clipboard you can use the API to find information on other open windows and paste directly.

    What exactly is your program? Tell us what and why you are trying to do and maybe there will be some suggestions.

  3. #3
    Lively Member
    Join Date
    Jan 2009
    Posts
    90

    Re: Paste text to any app with a text field?

    Might be a little hard for a first project...there are a few Windows API functions you need to use.

    First, you need to find the window handle that you are going to send text to. Windows aren't just the main window you see, each individual control (text box, command button, menu, etc.) is also a window and has a handle.

    Once you get the handle (usually abbreviated by 'hWnd'), you can use the SendMessage API function with the WM_PASTE message and that will tell that window to paste the contents of the clipboard.

    Start a new VB project and open a blank notepad document. Paste this code in the VB project and run it. Then look at the notepad window.

    You will need 2 textboxes (Text1 and Text2) and have some text in them.

    vb Code:
    1. Option Explicit
    2.  
    3. Private Const WM_PASTE = &H302
    4.  
    5. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    6. 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
    7. 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
    8.  
    9. Private Sub Form_Load()
    10.  
    11.     'Copy the contents of text1 and text2 to clipboard.
    12.     With Clipboard
    13.         .Clear
    14.         .SetText Text1.Text & Text2.Text, vbCFText
    15.     End With
    16.    
    17.     'Find the handle to an open notepad window.
    18.     Dim lonHandle As Long, lonEditHandle As Long
    19.    
    20.     'First parameter can be empty (vbNullString) if you don't know class name.
    21.     lonHandle = FindWindow("Notepad", "Untitled - Notepad")
    22.    
    23.     If lonHandle <> 0 Then 'If found, then...
    24.        
    25.         'We found the main window, now find the textbox window (class name "Edit").
    26.         lonEditHandle = FindWindowEx(lonHandle, 0, "Edit", vbNullString)
    27.        
    28.         If lonEditHandle <> 0 Then
    29.             SendMessage lonEditHandle, WM_PASTE, 0, ByVal 0&
    30.             MsgBox "Content pasted", vbInformation
    31.         Else
    32.             MsgBox "Cannot find edit window!", vbExclamation
    33.         End If
    34.        
    35.     Else
    36.         MsgBox "Cannot find open notepad window", vbExclamation
    37.     End If
    38.    
    39. End Sub

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    3

    Re: Paste text to any app with a text field?

    Thanks for the replies.
    Every account that gets opened at my work needs to have notes added, and they are usually a few standard comments or a combo of the few. I thought it would save me time if I could just hit a button and have it added.
    Guess it won't be so easy...I'll keep playing around and make some smaller easier things first while I'm learning.

    Thanks

  5. #5
    Addicted Member
    Join Date
    Jul 2007
    Posts
    228

    Re: Paste text to any app with a text field?

    Ahh... okay. What you need is a clipboard extender. A program where you can store frequently used text or graphics for pasting in external programs. Take a look at this program at Planet Source Code:

    http://www.planet-source-code.com/vb...69399&lngWId=1

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    3

    Re: Paste text to any app with a text field?

    That looks like it'll do what I need and more, thanks for the link.

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