Pasting text to another app's textbox

Hi,

I have two exe files in my project.
I need to open a form in the 2nd exe and transfer text to the first TextBox in the window from the 1st app. I can identify the Title/Classname of the 2nd app and make it the Foreground app, but I do not know how to select a dropdown menu, followed by the form I need to open, then insert the text.

I'd appreeciate a code sample, which shows me where I am going wrong.

I am using VB6.

Thank you kindly.

Kind regards,
   Tony
tvtechAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

watyCommented:
You can simulate keypress using the following class

' #VBIDEUtils#************************************************************
' * Programmer Name  : Scott Seligman
' * Web Site         : www.geocities.com/ResearchTriangle/6311/
' * E-Mail           : waty.thierry@usa.net
' * Date             : 18/10/1999
' * Time             : 10:45
' **********************************************************************
' * Comments         : Simulates keystrokes using keybd_event
' * This class simulates keystrokes using the keybd_event API.
' * If offers two advantages over SendKeys. First, it doesn't cause
' * the num lock light to flicker unless you specifically press the num lock key.
' * Secondly, it's possible to press and hold a key. Sample Usage:
' * Dim keys As New clsKeys
' * Dim bCapsLock As Boolean
' * keys.GetLockStatus bCapsLock, True, True
' * If bCapsLock Then
' *    keys.PressKeyVK keyCapsLock
' * End If
' * keys.PressString "Now is the time for all good men to come to the aid of their country."
' *
' **********************************************************************
'--------- Class Name: clsKeys

Option Explicit

Private Declare Function MapVirtualKey Lib "user32" Alias _
   "MapVirtualKeyA" (ByVal wCode As Long, _
   ByVal wMapType As Long) As Long

Private Declare Function VkKeyScan Lib "user32" Alias "VkKeyScanA" (ByVal _
   cChar As Byte) As Integer

Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal _
   bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As _
   Long) As Integer

Private Const KEYEVENTF_EXTENDEDKEY = &H1
Private Const KEYEVENTF_KEYUP = &H2

Public Enum enumKeys
   keyBackspace = &H8
   keyTab = &H9
   keyReturn = &HD
   keyShift = &H10
   keyControl = &H11
   keyAlt = &H12
   keyPause = &H13
   keyEscape = &H1B
   keySpace = &H20
   keyEnd = &H23
   keyHome = &H24
   keyLeft = &H25
   KeyUp = &H26
   keyRight = &H27
   KeyDown = &H28
   keyInsert = &H2D
   keyDelete = &H2E
   keyF1 = &H70
   keyF2 = &H71
   keyF3 = &H72
   keyF4 = &H73
   keyF5 = &H74
   keyF6 = &H75
   keyF7 = &H76
   keyF8 = &H77
   keyF9 = &H78
   keyF10 = &H79
   keyF11 = &H7A
   keyF12 = &H7B
   keyNumLock = &H90
   keyScrollLock = &H91
   keyCapsLock = &H14
End Enum

'Presses the single key represented by sKey
Public Sub PressKey(sKey As String, Optional bHold As Boolean, Optional _
   bRelease As Boolean)

   Dim nVK As Long
   nVK = VkKeyScan(Asc(sKey))

   If nVK = 0 Then
      Exit Sub
   End If

   Dim nScan As Long
   Dim nExtended As Long

   nScan = MapVirtualKey(nVK, 2)
   nExtended = 0
   If nScan = 0 Then
      nExtended = KEYEVENTF_EXTENDEDKEY
   End If
   nScan = MapVirtualKey(nVK, 0)

   Dim bShift As Boolean
   Dim bCtrl As Boolean
   Dim bAlt As Boolean

   bShift = (nVK And &H100)
   bCtrl = (nVK And &H200)
   bAlt = (nVK And &H400)

   nVK = (nVK And &HFF)

   If Not bRelease Then
      If bShift Then
         keybd_event enumKeys.keyShift, 0, 0, 0
      End If
      If bCtrl Then
         keybd_event enumKeys.keyControl, 0, 0, 0
      End If
      If bAlt Then
         keybd_event enumKeys.keyAlt, 0, 0, 0
      End If

      keybd_event nVK, nScan, nExtended, 0
   End If

   If Not bHold Then
      keybd_event nVK, nScan, KEYEVENTF_KEYUP Or nExtended, 0

      If bShift Then
         keybd_event enumKeys.keyShift, 0, KEYEVENTF_KEYUP, 0
      End If
      If bCtrl Then
         keybd_event enumKeys.keyControl, 0, KEYEVENTF_KEYUP, 0
      End If
      If bAlt Then
         keybd_event enumKeys.keyAlt, 0, KEYEVENTF_KEYUP, 0
      End If
   End If

End Sub

'Loop through a string and calls PressKey for each character (Does not
' parse strings like SendKeys)
Public Sub PressString(ByVal sString As String, Optional bDoEvents As Boolean = True)

   Do While sString <> ""
      PressKey Mid(sString, 1, 1)

      Sleep 20
      If bDoEvents Then
         DoEvents
      End If

      sString = Mid(sString, 2)
   Loop

End Sub

'Presses a specific key (this is used for keys that don't have a
' ascii equilivant)
Public Sub PressKeyVK(keyPress As enumKeys, Optional bHold As Boolean, _
   Optional bRelease As Boolean, Optional bCompatible As Boolean)

   Dim nScan As Long
   Dim nExtended As Long

   nScan = MapVirtualKey(keyPress, 2)
   nExtended = 0
   If nScan = 0 Then
      nExtended = KEYEVENTF_EXTENDEDKEY
   End If
   nScan = MapVirtualKey(keyPress, 0)

   If bCompatible Then
      nExtended = 0
   End If

   If Not bRelease Then
      keybd_event keyPress, nScan, nExtended, 0
   End If

   If Not bHold Then
      keybd_event keyPress, nScan, KEYEVENTF_KEYUP Or nExtended, 0
   End If

End Sub

'Returns (in the boolean variables) the status of the various Lock keys
Public Sub GetLockStatus(bCapsLock As Boolean, bNumLock As Boolean, _
   bScrollLock As Boolean)

   bCapsLock = GetKeyState(enumKeys.keyCapsLock)
   bNumLock = GetKeyState(enumKeys.keyNumLock)
   bScrollLock = GetKeyState(enumKeys.keyScrollLock)

End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
watyCommented:
You could also comunicate between your 2 app using DDE, here is a litle sample

' #VBIDEUtils#************************************************************
' * Programmer Name  : Waty Thierry
' * Web Site         : www.geocities.com/ResearchTriangle/6311/
' * E-Mail           : waty.thierry@usa.net
' * Date             : 28/06/99
' * Time             : 12:40
' **********************************************************************
' * Comments         : DDE Chat Example
' *
' *
' **********************************************************************

'***************************************************************
' Name: DDE Chat Example
' Description:This is a very small and simple example of how to m
'     ake two VB programs *running in the same PC* chat using DDE.
'This Is very useful For programming interprocess communication under Windows 9x/NT. Notice that LinkMode Property of the Form1 Is Set To '1 (Source) And the LinkItem Property of Text1 has been Set To Text1.
' By: Aldo Vargas
'
'
' Inputs:NONE
'
' Returns:The form being the client trap all the text in the fore
'     ing application.
'
'Assumes:1.Copy the code in a text file using Notepad, then renam
'     e the file to Form1.
'2.Add the form To a New project named Project1
'3.Compile the program And execute it twice.
'4.Press the Client button In one of the forms. WARNING: Be sure that both instances of the program are running, otherwise the program 'will crash.
'5.Now Select the other instance of the program And Type In the Text box. You will notice that all you Type Is reflected In the other 'instance of the program.
'6.If you want, run several intances of the program, push the Client button In all intances And Type any of them And see how all are 'updated automatically.
'
'Side Effects:-This program must be compiled to work properly.
'-At least 2 instances of the program must be running, otherwise it won't work.
'
'Code provided by Planet Source Code(tm) (http://www.PlanetSource
'     Code.com) 'as is', without warranties as to performance, fitness,
'     merchantability,and any other warranty (whether expressed or impl
'     ied).
'***************************************************************

'---- PASTE THE FOLLOWING CODE IN NOTEPAD AND RENAME THE FILE TO


'     Form1.frm
VERSION 5.00
Begin VB.Form Form1
Caption ="Form1"
ClientHeight=1740
ClientLeft =60
ClientTop=345
ClientWidth =3435
LinkMode=1 'Source
LinkTopic="Form1"
ScaleHeight =1740
ScaleWidth =3435
StartUpPosition =3 'Windows Default
Begin VB.CheckBox Check1
Caption ="Client"
Height =495
Left=225
Style=1 'Graphical
TabIndex=1
Top =1110
Width=1290
End
Begin VB.TextBox Text1
Height =825
Left=240
LinkItem="Text1"
MultiLine=-1 'True
TabIndex=0
Top =180
Width=2655
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False

Private Sub Check1_Click()
   
   If Check1 Then
      Text1.LinkMode = 0' Reset DDE
      Text1.LinkTopic = "Project1|Form1"' Start DDE conversation
      Text1.LinkItem = "Text1" ' Link to foreing object
      Text1.LinkMode = 3' Fire LinkNotify Event when foreing object changes
   End If
   
End Sub

Private Sub Text1_LinkNotify()
   
   Text1.LinkMode = 1 ' Refresh local control
   Text1.LinkMode = 3 ' Set control to fire LinkNotify Event again
   'Add your code here when foreing object changes
End Sub

0
asaflahv1Commented:
I have another idea...

Open two projects...
On the first one put:
a text box called - txtString
command button - cmdSend
command button - cmdQuit
and set the forms caption to - "Send String"
set the Forms Name to - "FrmSend"

On the second project put:
a Label - lblString
a command button - cmdQuit
and set the forms caption to - "Receive String"
set the Forms Name to - "frmReceive"

the cut and paste the following code:

on the sending project:
Option Explicit

Private Const WM_COPYDATA = &H4A

Private Type COPYDATASTRUCT
    dwData As Long
    cbData As Long
    lpData As Long
End Type

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
'
'Copies a block of memory from one location to another.
'
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
   (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
Private Sub cmdQuit_Click()
Unload Me
End Sub
Private Sub cmdSend_Click()
Dim sString As String
Dim lHwnd   As Long
Dim cds     As COPYDATASTRUCT
Dim buf(1 To 255) As Byte

sString = Trim$(txtString)
If sString = "" Then Exit Sub
'
' Get the handle of the target application.
'
lHwnd = FindWindow(vbNullString, "Receive String")
'
' Copy the string into a byte array,
' converting it to ASCII. Assign lpData
' the address of the byte array.
'
Call CopyMemory(buf(1), ByVal sString, Len(sString))
With cds
    .dwData = 3
    .cbData = Len(sString) + 1
    .lpData = VarPtr(buf(1))
End With
'
' Send the string.
'
Call SendMessage(lHwnd, WM_COPYDATA, Me.hwnd, cds)
End Sub


on the recieving project:
Option Explicit

Public lpPrevWndProc As Long
Public lHwnd         As Long

Const GWL_WNDPROC = (-4)
Const WM_COPYDATA = &H4A

Public Type COPYDATASTRUCT
    dwData As Long
    cbData As Long
    lpData As Long
End Type
'
'Copies a block of memory from one location to another.
'
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
   (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)

Declare Function CallWindowProc Lib "user32" Alias _
   "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As _
   Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As _
   Long) As Long

Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
   (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As _
   Long) As Long
Public Sub pHook()
'
' Sub class the form to trap for Windows messages.
'
lpPrevWndProc = SetWindowLong(lHwnd, GWL_WNDPROC, AddressOf fWindowProc)
Debug.Print lpPrevWndProc
End Sub
Sub pReceiveMsg(lParam As Long)
Dim sString As String
Dim cds     As COPYDATASTRUCT
Dim buf(1 To 255) As Byte
'
' Copy the data sent to this application
' into a local structure.
'
Call CopyMemory(cds, ByVal lParam, Len(cds))
'
' Copy the string that was passed into a byte array.
'
Call CopyMemory(buf(1), ByVal cds.lpData, cds.cbData)
'
' Convert the ASCII byte array back to a Unicode string.
'
sString = StrConv(buf, vbUnicode)
sString = Left$(sString, InStr(1, sString, Chr$(0)) - 1)
'
' Display the received string.
'
frmReceive.lblString = sString
End Sub
Public Sub pUnhook()
'
' Remove the subclassing.
'
Call SetWindowLong(lHwnd, GWL_WNDPROC, lpPrevWndProc)
End Sub
Function fWindowProc(ByVal hw As Long, ByVal uMsg As Long, _
         ByVal wParam As Long, ByVal lParam As Long) As Long
'
' This callback routine is called by Windows whenever
' a message is sent to this form.  If it is the copy
' message call our procedure to receive the message.
'
If uMsg = WM_COPYDATA Then Call pReceiveMsg(lParam)
'
' Call the original window procedure associated with this form.
'
fWindowProc = CallWindowProc(lpPrevWndProc, hw, uMsg, wParam, lParam)
End Function




Private Sub cmdQuit_Click()
Unload Me
End Sub

Private Sub Form_Load()
'
' Get this form's handle.
' Subclass this form to trap Windows messages.
'
lHwnd = Me.hwnd
Call pHook
End Sub

Private Sub Form_Unload(Cancel As Integer)
'
' Un-subclass the form.
'
Call pUnhook
End Sub


GOOD LUCK!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.