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)?
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.
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:
Option Explicit
Private Const WM_PASTE = &H302
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
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
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 Sub Form_Load()
'Copy the contents of text1 and text2 to clipboard.
With Clipboard
.Clear
.SetText Text1.Text & Text2.Text, vbCFText
End With
'Find the handle to an open notepad window.
Dim lonHandle As Long, lonEditHandle As Long
'First parameter can be empty (vbNullString) if you don't know class name.
lonHandle = FindWindow("Notepad", "Untitled - Notepad")
If lonHandle <> 0 Then 'If found, then...
'We found the main window, now find the textbox window (class name "Edit").
lonEditHandle = FindWindowEx(lonHandle, 0, "Edit", vbNullString)
If lonEditHandle <> 0 Then
SendMessage lonEditHandle, WM_PASTE, 0, ByVal 0&
MsgBox "Content pasted", vbInformation
Else
MsgBox "Cannot find edit window!", vbExclamation
End If
Else
MsgBox "Cannot find open notepad window", vbExclamation
End If
End Sub
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
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
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.