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.
Printable View
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?Quote:
Originally Posted by ed_kaska
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.
Declare these
VB Code:
Option Explicit 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 Const WM_SETTEXT = &HC
Then here is how you do it
VB Code:
Dim hWndparent As Long Dim hWndchild As Long Dim ProgramCaption As String Dim TextBoxClass As String Dim TextBoxCaption As String Dim txtToSend As String 'get values of TextboxClass and TextBoxCaption from Spy++ 'set ProgramCaption to the Caption in the external program 'set texttosend to the text your want to put in the text box 'Get handle to program you want to send text to hWndparent = FindWindow(vbNullString, ProgramCaption) 'get handle to textbox hWndchild = FindWindowEx(hWndparent, 0, TextBoxClass, TextBoxCaption) 'send text Call SendMessage(hWndchild, WMSETTExt, 0, txtToSend)
...
Can I embed "tab" characters to navigate fields in the text string?
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:
Option Explicit 'API functions you'll need Public Declare Function SetForegroundWindow Lib "user32" ( _ ByVal hwnd As Long _ ) As Long Public Declare Function ShowWindow Lib "user32" ( _ ByVal hwnd As Long, _ ByVal nCmdShow As Long _ ) As Long Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _ (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Public Declare Function SendInput Lib "user32.dll" ( _ ByVal nInputs As Long, _ pInputs As GENERALINPUT, _ ByVal cbSize As Long _ ) As Long Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _ pDst As Any, _ pSrc As Any, _ ByVal ByteLen As Long _ ) 'Constants for ShowWindow Public Const SW_HIDE = 0 Public Const SW_MAXIMIZE = 3 Public Const SW_MINIMIZE = 6 Public Const SW_NORMAL = 1 Public Const SW_RESTORE = 9 Public Const SW_SHOW = 5 'types and constants for SendInput Private Type MOUSEINPUT dx As Long dy As Long mouseData As Long dwFlags As Long time As Long dwExtraInfo As Long End Type Private Type KEYBDINPUT wVk As Integer wScan As Integer dwFlags As Long time As Long dwExtraInfo As Long End Type Private Type HARDWAREINPUT uMsg As Long wParamL As Integer wParamH As Integer End Type Private Type GENERALINPUT dwType As Long xi(0 To 23) As Byte End Type Public Const KEYEVENTF_KEYUP = &H2 Public Const INPUT_MOUSE = 0 Public Const INPUT_KEYBOARD = 1 Public Const INPUT_HARDWARE = 2 'Virtual Keys, Standard Set Public Const VK_LBUTTON = &H1 Public Const VK_RBUTTON = &H2 Public Const VK_CANCEL = &H3 Public Const VK_MBUTTON = &H4 ' NOT contiguous with L RBUTTON Public Const VK_BACK = &H8 Public Const VK_TAB = &H9 Public Const VK_CLEAR = &HC Public Const VK_RETURN = &HD Public Const VK_SHIFT = &H10 Public Const VK_CONTROL = &H11 Public Const VK_MENU = &H12 Public Const VK_PAUSE = &H13 Public Const VK_CAPITAL = &H14 Public Const VK_ESCAPE = &H1B Public Const VK_SPACE = &H20 Public Const VK_PRIOR = &H21 Public Const VK_NEXT = &H22 Public Const VK_END = &H23 Public Const VK_HOME = &H24 Public Const VK_LEFT = &H25 Public Const VK_UP = &H26 Public Const VK_RIGHT = &H27 Public Const VK_DOWN = &H28 Public Const VK_SELECT = &H29 Public Const VK_PRINT = &H2A Public Const VK_EXECUTE = &H2B Public Const VK_SNAPSHOT = &H2C Public Const VK_INSERT = &H2D Public Const VK_DELETE = &H2E Public Const VK_HELP = &H2F 'VK_A thru VK_Z are the same as their ASCII equivalents: 'A' thru 'Z' 'VK_0 thru VK_9 are the same as their ASCII equivalents: '0' thru '9' Public Const VK_NUMPAD0 = &H60 Public Const VK_NUMPAD1 = &H61 Public Const VK_NUMPAD2 = &H62 Public Const VK_NUMPAD3 = &H63 Public Const VK_NUMPAD4 = &H64 Public Const VK_NUMPAD5 = &H65 Public Const VK_NUMPAD6 = &H66 Public Const VK_NUMPAD7 = &H67 Public Const VK_NUMPAD8 = &H68 Public Const VK_NUMPAD9 = &H69 Public Const VK_MULTIPLY = &H6A Public Const VK_ADD = &H6B Public Const VK_SEPARATOR = &H6C Public Const VK_SUBTRACT = &H6D Public Const VK_DECIMAL = &H6E Public Const VK_DIVIDE = &H6F Public Const VK_F1 = &H70 Public Const VK_F2 = &H71 Public Const VK_F3 = &H72 Public Const VK_F4 = &H73 Public Const VK_F5 = &H74 Public Const VK_F6 = &H75 Public Const VK_F7 = &H76 Public Const VK_F8 = &H77 Public Const VK_F9 = &H78 Public Const VK_F10 = &H79 Public Const VK_F11 = &H7A Public Const VK_F12 = &H7B Public Const VK_F13 = &H7C Public Const VK_F14 = &H7D Public Const VK_F15 = &H7E Public Const VK_F16 = &H7F Public Const VK_F17 = &H80 Public Const VK_F18 = &H81 Public Const VK_F19 = &H82 Public Const VK_F20 = &H83 Public Const VK_F21 = &H84 Public Const VK_F22 = &H85 Public Const VK_F23 = &H86 Public Const VK_F24 = &H87 Public Const VK_NUMLOCK = &H90 Public Const VK_SCROLL = &H91 ' VK_L VK_R - left and right Alt, Ctrl and Shift virtual keys. ' Used only as parameters to GetAsyncKeyState() and GetKeyState(). ' No other API or message will distinguish left and right keys in this way. Public Const VK_LSHIFT = &HA0 Public Const VK_RSHIFT = &HA1 Public Const VK_LCONTROL = &HA2 Public Const VK_RCONTROL = &HA3 Public Const VK_LMENU = &HA4 Public Const VK_RMENU = &HA5 Public Const VK_ATTN = &HF6 Public Const VK_CRSEL = &HF7 Public Const VK_EXSEL = &HF8 Public Const VK_EREOF = &HF9 Public Const VK_PLAY = &HFA Public Const VK_ZOOM = &HFB Public Const VK_NONAME = &HFC Public Const VK_PA1 = &HFD Public Const VK_OEM_CLEAR = &HFE 'This public routine presses two Tabs and a return on 'whatever has focus in the Program Window Public Sub DoIt(ProgramCaption As String) 'get handle to window hWindow = FindWindow(vbNullString, ProgramCaption) 'show it ShowWindow hWindow, SW_SHOW 'make sure it is foreground window SetForegroundWindow hWindow 'tab over to cancel button PressKey VK_TAB, False PressKey VK_TAB, False 'and press it PressKey VK_RETURN, False End Sub Private Sub PressKey(vKeyCode As Byte, Optional ShiftState As Boolean) If ShiftState Then _ SendKey VK_SHIFT, 0 SendKey vKeyCode, 0 SendKey vKeyCode, KEYEVENTF_KEYUP If ShiftState Then _ SendKey VK_SHIFT, KEYEVENTF_KEYUP End Sub Private Sub SendKey(bKey As Byte, UpDown As Integer) Dim GInput As GENERALINPUT Dim KInput As KEYBDINPUT KInput.wVk = bKey ' the key we're going to release KInput.dwFlags = UpDown ' release the key GInput.dwType = INPUT_KEYBOARD ' keyboard input CopyMemory GInput.xi(0), KInput, Len(KInput) Call SendInput(1, GInput, Len(GInput)) End Sub
Hi Moeur,
I am using your line of code:
VB Code:
Call SendMessage(hWndchild, WMSETTExt, 0, txtToSend)
...and I have a little problem.. weird one, here's the code I used:
...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...VB Code:
Call SendMessage("2425814", WM_SETTEXT, 0, "Hello!!!")
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
I have two problems with thisQuote:
Call SendMessage("2425814", WM_SETTEXT, 0, "Hello!!!")
First of all, the handle is a long value so you should have
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.VB Code:
Call SendMessage(2425814&, WM_SETTEXT, 0, "Hello!!!")
fourQuote:
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
Hi Moeur, I'd need some more help from you...
Still on the following line of code:
VB Code:
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
VB Code:
Call SendMessage(2950226, WM_SETTEXT, 0, ByVal "Hello!")
should work now lol ive had the same prob lol
That works perfectly... thanks to all of you