Results 1 to 12 of 12

Thread: sending text to other applications

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2004
    Location
    Minneapolis, MN
    Posts
    17

    sending text to other applications

    Hi:
    Can anyone guide me to some good simple example of sending text to another application using either sendkeys in VBA for EXCEL or send message or set focus or whatever to put some text into a textbox in a different application.
    Thanks.
    Ed Kaska

  2. #2
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: sending text to other applications

    Quote Originally Posted by ed_kaska
    Hi:
    Can anyone guide me to some good simple example of sending text to another application using either sendkeys in VBA for EXCEL or send message or set focus or whatever to put some text into a textbox in a different application.
    Thanks.
    Did you make the other application? or is it an application such as IE, A game etc?

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2004
    Location
    Minneapolis, MN
    Posts
    17

    Re: sending text to other applications

    It is not a game or IE. It is a local application that manages construction loans. Uses text boxes, combo boxes, etc. I am just concerned with taking the loan numbers out of a spread sheet and using the loan number to plug in to the pricipal receipt screen to bring up the account. Should eliminate about 4 mounse clicks and repetive keyboard input for the same loan number over and over again.
    Thanks.
    Ed Kaska

  4. #4
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: sending text to other applications

    Declare these
    VB Code:
    1. Option Explicit
    2. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
    3.     ByVal lpClassName As String, _
    4.     ByVal lpWindowName As String _
    5. ) As Long
    6.  
    7. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
    8.     ByVal hwnd As Long, _
    9.     ByVal wMsg As Long, _
    10.     ByVal wParam As Long, _
    11.     lParam As Any _
    12. ) As Long
    13.  
    14. Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" ( _
    15.     ByVal hWnd1 As Long, _
    16.     ByVal hWnd2 As Long, _
    17.     ByVal lpsz1 As String, _
    18.     ByVal lpsz2 As String _
    19. ) As Long
    20.  
    21. Private Const WM_SETTEXT = &HC

    Then here is how you do it

    VB Code:
    1. Dim hWndparent As Long
    2. Dim hWndchild As Long
    3. Dim ProgramCaption As String
    4. Dim TextBoxClass As String
    5. Dim TextBoxCaption As String
    6. Dim txtToSend As String
    7.  
    8. 'get values of TextboxClass and TextBoxCaption from Spy++
    9. 'set ProgramCaption to the Caption in the external program
    10. 'set texttosend to the text your want to put in the text box
    11.  
    12.  
    13. 'Get handle to program you want to send text to
    14. hWndparent = FindWindow(vbNullString, ProgramCaption)
    15.  
    16. 'get handle to textbox
    17. hWndchild = FindWindowEx(hWndparent, 0, TextBoxClass, TextBoxCaption)
    18.  
    19. 'send text
    20. Call SendMessage(hWndchild, WMSETTExt, 0, txtToSend)

    ...

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2004
    Location
    Minneapolis, MN
    Posts
    17

    Re: sending text to other applications

    Can I embed "tab" characters to navigate fields in the text string?
    Ed Kaska

  6. #6
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: sending text to other applications

    I don't think so, try it.

    To simulate pressing keys in another app then you could do like the following DoIt routine does.
    VB Code:
    1. Option Explicit
    2.  
    3. 'API functions you'll need
    4. Public Declare Function SetForegroundWindow Lib "user32" ( _
    5.     ByVal hwnd As Long _
    6. ) As Long
    7.  
    8. Public Declare Function ShowWindow Lib "user32" ( _
    9.     ByVal hwnd As Long, _
    10.     ByVal nCmdShow As Long _
    11. ) As Long
    12.  
    13. Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    14.   (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    15.  
    16. Public Declare Function SendInput Lib "user32.dll" ( _
    17.     ByVal nInputs As Long, _
    18.     pInputs As GENERALINPUT, _
    19.     ByVal cbSize As Long _
    20. ) As Long
    21.  
    22. Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
    23.     pDst As Any, _
    24.     pSrc As Any, _
    25.     ByVal ByteLen As Long _
    26. )
    27.  
    28. 'Constants for ShowWindow
    29. Public Const SW_HIDE = 0
    30. Public Const SW_MAXIMIZE = 3
    31. Public Const SW_MINIMIZE = 6
    32. Public Const SW_NORMAL = 1
    33. Public Const SW_RESTORE = 9
    34. Public Const SW_SHOW = 5
    35.  
    36.  
    37. 'types and constants for SendInput
    38. Private Type MOUSEINPUT
    39.   dx As Long
    40.   dy As Long
    41.   mouseData As Long
    42.   dwFlags As Long
    43.   time As Long
    44.   dwExtraInfo As Long
    45. End Type
    46.  
    47. Private Type KEYBDINPUT
    48.   wVk As Integer
    49.   wScan As Integer
    50.   dwFlags As Long
    51.   time As Long
    52.   dwExtraInfo As Long
    53. End Type
    54.  
    55. Private Type HARDWAREINPUT
    56.   uMsg As Long
    57.   wParamL As Integer
    58.   wParamH As Integer
    59. End Type
    60.  
    61. Private Type GENERALINPUT
    62.   dwType As Long
    63.   xi(0 To 23) As Byte
    64. End Type
    65.  
    66. Public Const KEYEVENTF_KEYUP = &H2
    67. Public Const INPUT_MOUSE = 0
    68. Public Const INPUT_KEYBOARD = 1
    69. Public Const INPUT_HARDWARE = 2
    70.            
    71. 'Virtual Keys, Standard Set
    72. Public Const VK_LBUTTON = &H1
    73. Public Const VK_RBUTTON = &H2
    74. Public Const VK_CANCEL = &H3
    75. Public Const VK_MBUTTON = &H4  '  NOT contiguous with L RBUTTON
    76.  
    77. Public Const VK_BACK = &H8
    78. Public Const VK_TAB = &H9
    79.  
    80. Public Const VK_CLEAR = &HC
    81. Public Const VK_RETURN = &HD
    82.  
    83. Public Const VK_SHIFT = &H10
    84. Public Const VK_CONTROL = &H11
    85. Public Const VK_MENU = &H12
    86. Public Const VK_PAUSE = &H13
    87. Public Const VK_CAPITAL = &H14
    88. Public Const VK_ESCAPE = &H1B
    89. Public Const VK_SPACE = &H20
    90. Public Const VK_PRIOR = &H21
    91. Public Const VK_NEXT = &H22
    92. Public Const VK_END = &H23
    93. Public Const VK_HOME = &H24
    94. Public Const VK_LEFT = &H25
    95. Public Const VK_UP = &H26
    96. Public Const VK_RIGHT = &H27
    97. Public Const VK_DOWN = &H28
    98. Public Const VK_SELECT = &H29
    99. Public Const VK_PRINT = &H2A
    100. Public Const VK_EXECUTE = &H2B
    101. Public Const VK_SNAPSHOT = &H2C
    102. Public Const VK_INSERT = &H2D
    103. Public Const VK_DELETE = &H2E
    104. Public Const VK_HELP = &H2F
    105.  
    106. 'VK_A thru VK_Z are the same as their ASCII equivalents: 'A' thru 'Z'
    107. 'VK_0 thru VK_9 are the same as their ASCII equivalents: '0' thru '9'
    108.  
    109. Public Const VK_NUMPAD0 = &H60
    110. Public Const VK_NUMPAD1 = &H61
    111. Public Const VK_NUMPAD2 = &H62
    112. Public Const VK_NUMPAD3 = &H63
    113. Public Const VK_NUMPAD4 = &H64
    114. Public Const VK_NUMPAD5 = &H65
    115. Public Const VK_NUMPAD6 = &H66
    116. Public Const VK_NUMPAD7 = &H67
    117. Public Const VK_NUMPAD8 = &H68
    118. Public Const VK_NUMPAD9 = &H69
    119. Public Const VK_MULTIPLY = &H6A
    120. Public Const VK_ADD = &H6B
    121. Public Const VK_SEPARATOR = &H6C
    122. Public Const VK_SUBTRACT = &H6D
    123. Public Const VK_DECIMAL = &H6E
    124. Public Const VK_DIVIDE = &H6F
    125. Public Const VK_F1 = &H70
    126. Public Const VK_F2 = &H71
    127. Public Const VK_F3 = &H72
    128. Public Const VK_F4 = &H73
    129. Public Const VK_F5 = &H74
    130. Public Const VK_F6 = &H75
    131. Public Const VK_F7 = &H76
    132. Public Const VK_F8 = &H77
    133. Public Const VK_F9 = &H78
    134. Public Const VK_F10 = &H79
    135. Public Const VK_F11 = &H7A
    136. Public Const VK_F12 = &H7B
    137. Public Const VK_F13 = &H7C
    138. Public Const VK_F14 = &H7D
    139. Public Const VK_F15 = &H7E
    140. Public Const VK_F16 = &H7F
    141. Public Const VK_F17 = &H80
    142. Public Const VK_F18 = &H81
    143. Public Const VK_F19 = &H82
    144. Public Const VK_F20 = &H83
    145. Public Const VK_F21 = &H84
    146. Public Const VK_F22 = &H85
    147. Public Const VK_F23 = &H86
    148. Public Const VK_F24 = &H87
    149. Public Const VK_NUMLOCK = &H90
    150. Public Const VK_SCROLL = &H91
    151.  
    152. '   VK_L VK_R - left and right Alt, Ctrl and Shift virtual keys.
    153. '   Used only as parameters to GetAsyncKeyState() and GetKeyState().
    154. '   No other API or message will distinguish left and right keys in this way.
    155.  
    156. Public Const VK_LSHIFT = &HA0
    157. Public Const VK_RSHIFT = &HA1
    158. Public Const VK_LCONTROL = &HA2
    159. Public Const VK_RCONTROL = &HA3
    160. Public Const VK_LMENU = &HA4
    161. Public Const VK_RMENU = &HA5
    162. Public Const VK_ATTN = &HF6
    163. Public Const VK_CRSEL = &HF7
    164. Public Const VK_EXSEL = &HF8
    165. Public Const VK_EREOF = &HF9
    166. Public Const VK_PLAY = &HFA
    167. Public Const VK_ZOOM = &HFB
    168. Public Const VK_NONAME = &HFC
    169. Public Const VK_PA1 = &HFD
    170. Public Const VK_OEM_CLEAR = &HFE
    171.            
    172. 'This public routine presses two Tabs and a return on
    173. 'whatever has focus in the Program Window            
    174. Public Sub DoIt(ProgramCaption As String)
    175.             'get handle to window
    176.             hWindow = FindWindow(vbNullString, ProgramCaption)
    177.             'show it
    178.             ShowWindow hWindow, SW_SHOW
    179.             'make sure it is foreground window
    180.             SetForegroundWindow hWindow
    181.             'tab over to cancel button
    182.             PressKey VK_TAB, False
    183.             PressKey VK_TAB, False
    184.             'and press it
    185.             PressKey VK_RETURN, False
    186. End Sub
    187.  
    188. Private Sub PressKey(vKeyCode As Byte, Optional ShiftState As Boolean)
    189.  If ShiftState Then _
    190.     SendKey VK_SHIFT, 0
    191.  SendKey vKeyCode, 0
    192.  SendKey vKeyCode, KEYEVENTF_KEYUP
    193.  If ShiftState Then _
    194.     SendKey VK_SHIFT, KEYEVENTF_KEYUP
    195. End Sub
    196.  
    197. Private Sub SendKey(bKey As Byte, UpDown As Integer)
    198.     Dim GInput As GENERALINPUT
    199.     Dim KInput As KEYBDINPUT
    200.     KInput.wVk = bKey  ' the key we're going to release
    201.     KInput.dwFlags = UpDown  ' release the key
    202.     GInput.dwType = INPUT_KEYBOARD  ' keyboard input
    203.     CopyMemory GInput.xi(0), KInput, Len(KInput)
    204.     Call SendInput(1, GInput, Len(GInput))
    205. End Sub

  7. #7
    Hyperactive Member Krass's Avatar
    Join Date
    Aug 2000
    Location
    Montreal
    Posts
    489

    Re: sending text to other applications

    Hi Moeur,

    I am using your line of code:
    VB Code:
    1. Call SendMessage(hWndchild, WMSETTExt, 0, txtToSend)

    ...and I have a little problem.. weird one, here's the code I used:
    VB Code:
    1. Call SendMessage("2425814", WM_SETTEXT, 0, "Hello!!!")
    ...where "2425814" is the hwnd of the textbox (running in another vb app). When sending "Hello!!!" that way, it fills the textbox with 2-3-4 weird ascii characters...

    I then tried sending asc("Hello!!!") and only the first letter appears in the textbox! (Whatever word I use... "Goodbye" would put a "G" in there)....

    Any idea?

    Thanks
    Chris

  8. #8
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: sending text to other applications

    Call SendMessage("2425814", WM_SETTEXT, 0, "Hello!!!")
    I have two problems with this
    First of all, the handle is a long value so you should have
    VB Code:
    1. Call SendMessage(2425814&, WM_SETTEXT, 0, "Hello!!!")
    The second problem I have is that you cannot know what the handle is each time you want to use it because it changes, so use FindWindow and FindWindowEX to get the handle each time.

  9. #9
    Fanatic Member TTn's Avatar
    Join Date
    Jul 2004
    Posts
    708

    Re: sending text to other applications

    Option explicit
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
    ByVal lpClassName As String, _
    ByVal lpWindowName As String) As Long
    Private Declare Function ShowWindow Lib "user32" ( _
    ByVal hwnd As Long, _
    ByVal nCmdShow As Long) As Long
    Private Declare Function GetDesktopWindow Lib "user32" ( _
    ) As Long
    Private Declare Function GetForegroundWindow Lib "user32" ( _
    ) As Long
    Private Declare Function LockWindowUpdate Lib "user32" ( _
    ByVal hwndLock As Long) As Long
    Private Declare Function BlockInput Lib "user32" ( _
    ByVal fBlock As Long) As Long

    Private Sub Button1_Click()
    Dim hWndApp As Long
    On Error Resume Next

    'Block input and get current focus
    BlockInput True
    LockWindowUpdate (GetDesktopWindow)
    txtForeground.Text = CStr(GetForegroundWindow)

    'find window by inserting app's caption name.
    hWndApp = FindWindow(vbNullString, "AppNameCaption")

    ' this shows it
    ShowWindow hWndApp, 1

    'send combination of keys
    Sendkeys ("{Tab}hello{Tab}{enter}")

    'unblock and return focus
    BlockInput False
    LockWindowUpdate False
    SetForegroundWindow (CLng(txtForeground.Text))
    End Sub
    four

  10. #10
    Hyperactive Member Krass's Avatar
    Join Date
    Aug 2000
    Location
    Montreal
    Posts
    489

    Re: sending text to other applications

    Hi Moeur, I'd need some more help from you...

    Still on the following line of code:
    VB Code:
    1. Call SendMessage(2950226, WM_SETTEXT, 0, "Hello!")

    I know the hwnd changes dynamically, the hwnd used in my code sample (2950226) is typed in manually for test purposes - I know the handle is good since the sendmessage command changes the value of the textbox.

    My main problem is that the content of the textbox doesn't change to "Hello!" like it should. In debugging mode, I've executed the SendMessage command several times, and here's what it send to the other app's textbox: LÕh <OR> „l <OR> ô¢m <OR> such weird series of ascii characters.

    Also, I've tried using "&" at the end of the hwnd, like you suggested, but VB just takes if off. Then again, I believe that part is okay (?) since the textbox content gets modified.

    Would you have an idea on how to solve this?

    Thanks
    Chris

  11. #11
    New Member
    Join Date
    Apr 2005
    Posts
    7

    Re: sending text to other applications

    VB Code:
    1. Call SendMessage(2950226, WM_SETTEXT, 0, ByVal "Hello!")

    should work now lol ive had the same prob lol

  12. #12
    Hyperactive Member Krass's Avatar
    Join Date
    Aug 2000
    Location
    Montreal
    Posts
    489

    Re: sending text to other applications

    That works perfectly... thanks to all of you
    Chris

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