Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I put text in another application's text control?

Status
Not open for further replies.

MAKNIGHT71

IS-IT--Management
Sep 25, 2003
67
US
I am writing VBA code (in Exact Macola) that needs to place text in a text control in a seperate VB .NET 2003 application. I can use FindWindow sucessfully to get the application's main window, however, I am not having any luck whatsoever using FindWindowEx to find the text control itself so that I can call SendMessage. Right now I'm changing the .NET application's main window text and using a timer to notice this change and feed the text control. This is kinda cheesy, but I had to get something working quickly. Ideas on directly feeding the text box?

maknight
 
If you have control over the design of the "other" application, you might be able use a label with an accelerator key (the ampersand = &) to identify the text box, and then send an Alt- sequence to cause focus to go to the text box. At that point you should be able to send keys to the text box.

 
I wrote the other application, so I know the exact name of the textbox and it's associated label text. But, it still is playing dumb and getting the handle for the edit control. I cannot use SendKeys b/c as soon as i call it (after an AppActivate call), focus returns immediately to the VBA application if another function is called. I tried in a timer with the same result.

' If application is already running, send the item number to it
Dim hWnd As Long
Dim hEdit As Long
On Error GoTo LoadMyApp
hWnd = FindWindow(vbNullString, "My Program Name")
hEdit = FindWindowEx(hWnd, 0, vbNullString, "&Part Number:")
Call SendMessage(hEdit, WM_SETTEXT, 0, ByVal ItemNo.Text)
AppActivate ItemNo.Text
Exit Sub

If I pass vbNullString as the fourth parameter as well, it successfully returns a handle to the fourth (in the tab order) control on the dialog (a status bar).

maknight
 
I had a situation where I was using a separate .exe file for handling some of the processing on behalf of a main program. I used a simple protocol that goes something like this:

Two programs: call them Main and Sub...

1. Main launches Sub and includes Main's window handle in the parameter string.

2. Sub posts a message to Main providing Sub's window handle.

3. Main can now post messages to Sub and pass whatever instruction is required. E.g. wParam = request, lParam = data.

All you have to do is define your own message numbers (Offset from WM_USER) and away you go!

The reason WM_SETTEXT isn't working for you is that the lParam ("fouth parameter") is expected to be a pointer to a string. You might try playing around with allocating memory, putting your value in that memory location and passing the pointer. Don't forget to free the memory when no longer needed. (Since you are using SendMessage, you can free the memory immediately since you don't get control back until the message has been processed by the other program.)

 
I figured out what my problem was after running Spy++. Why I didn't try that sooner, I just don't know. The text box is within a group box, so I had to drill down to the group box's handle and get the text box from there. NOW it works!!

I don't recall having to do that in some older VC++ apps I wrote, but I could very well be wrong.

Thanks for your pointers. Below is the working code...

Dim hWnd As Long, hEdit As Long, hGroup As Long
On Error GoTo LoadMyApp
hWnd = FindWindow(vbNullString, "My Application") ' get the main app window handle
hGroup = FindWindowEx(hWnd, 0, "WindowsForms10.Window.8.app6", "") ' get the group box window handle
hEdit = FindWindowEx(hGroup, 0, "WindowsForms10.EDIT.app6", vbNullString) ' get the text box window handle
Call SendMessage(hEdit, WM_SETTEXT, 0, ByVal ItemNo.Text) ' send the part number to the text field
Call SendMessage(hWnd, WM_KEYDOWN, vbKeyF7, 0) ' send an F7 key to start search (event in app)
Call SendMessage(hEdit, WM_SETFOCUS, 0, 0) ' send msg to set focus to force text color change (event in app)
AppActivate "My Application" ' bring the app to the front


maknight
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top