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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Possible to use SendMessage with form in external application?

Status
Not open for further replies.

Clark23

Technical User
Apr 6, 2006
6
US
I have some 3rd party database software that I am working with. I am using VBA from Excel to open the software. To my knowledge there is no way to pass SQL script to the application via the command line and the documentation is extremely limited. There are several things I would need to do from VBA.

1. Open the application (done)
2. Double-click or simulate opening of an icon in a child window inside the application
3. Pass a string into a form that pops up when clicking the icon (i.e. "11123AB-C")
4. "Submit" the form (press a command button)

Basically I am trying to automate some database queries.

Is this even possible? If so, I'd appreciate it if someone can just point me in the right direction.

If I have to use KeyPress, that's okay, because I wouldn't be putting in THAT many values, and speed of the code is not a big priority.

Thanks in advance for any help!
 
By the way, the application in question is STS SmartLook if anyone has ever heard of it. I google'd just about every possible thing I could think of to find documentation for it -- no luck! I imagine there should be some way to manually pass SQL query via command line which would probably make this a thousand times easier! :)
 
Hi
I have done these things before but in Main
Visual Basic . The following link shows
how to do a post message in VBA.

What you need to do for ANY windows application
in order to send or modify its behaviour
is to use the SPY++ application found in the visual
studio or download alternative spy apps from the
internet.

You need to spy on the application child windows
or main windows and look for MOUSE CLICK messages.

In your case you need to spy on
all the window handles belonging to
the main application for the following
messages. Set the FILTER to show only

WM_LBUTTONDOWN
WM_LBUTTONUP
WM_LBUTTONDBLCLICK

Move your mouse over the ICON and do a left click.

Identify which window handle shows these messages.
With SPY, Close all other spy windows except
the ones you identified and re-spy to verify.
Copy down the Wparam and the Lparam values
It will look something like

handle message wparam, Lparam
=====================================
A95394, WM_LBUTTONDOWN, 1,70032
A95394, WM_LBUTTONUP, 0,70032


use POSTMESSAGE to post them back to the windows
handle.. in VBA.

In order to get back to the same window handle
you need to observe the window tree structure
and use GetWindow() to move up and down the
chain so that your VBA code will be pointing
back to the same window handle. Then you
send it with postmessage.

postmessage(hwnd,WM_LBUTTONDOWN,wparam,lparam)
postmessage(hwnd,WM_LBUTTONUP,wparam,lparam)

This will cause the Windows OS to simulate
a mouse press over that Icon in that window.

Basically you are PLAYING BACK what you observed with
SPY.

To change the text of the control you use
a postmessage or the SETWINDOWTEXT() api
to change the text.

The window handles change everytime you close
and start any windows app.


Here is the fancy VB code to traverse a Window Tree
to look for the text and classes so that your
code will point to the right handle.


In my code I normally
control the app by shutting it down and re-running it
so that it goes back into a known state. Then
I use the getwindow(hwnd,child) nextchild to crawl
down the window tree..

How do I know where to stop?
I use SPY to see how many times I need to go down
the tree to get to the correct window handle. It will
ALWAYS be the same as long as you can control the initial
state of your external app.

You can TEST first by writing a small VBA app to
do a postmessage to see if it will work without
all the extra code.

1)Use SPY to get the window handle of
a control that responds to the a mouse click.
(use some other simple app like notepad.exe)

2) Copy the windows handle of that control
into your vba code and run it to
see if the mouse will click on the app.
You need to arrange your app so that you can see
with your eyes if things are working.

PostMessage(handle, WM_LBUTTONDOWN, 1,&H70032 )
PostMessage(handle, WM_LBUTTONUP, 0, &H70032)
DOEVENTS
DOEVENTS

Once that works you need to modify your
app to crawl back to the same control
when your app is running and send the messages.

Do not use SENDMESSAGE as it waits for a response.
Use postmessage so that the system and your code
can continue executing.

Aprivate.
 
I forgot to mention,

The 'lparam' eg (&H70032)
PostMessage(handle, WM_LBUTTONDOWN, 1,&H70032 )

Will change everytime you re-spy on the windows
by repeating the mouse click. Thats because
you can hardly place the mouse back over the same
spot. To play safe, place the mouse over the CENTER
of the ICON or button so that any value you copy
will be the approximate mouse pointer position over the center.

The Lparam contains the X,Y position of the
window in the app you want to control relative
to the apps own parent window.
For for the
ICON you want to click,The ICon is located within
a specific Child Window and the X,Y position is
referring to the position of the Icon where your
mouse clicked inside the child window.
If you move the parent window (STS program)
elsewhere on the windows Desktop , the program will
still work.


Aprivate




 
Wow thanks for taking the time to explain it with such a lengthy reply. I had kind of given up on this and resorted to keying in everything manually but this will save me a ton of time if I can get it all together! :)

I'll let you know how it goes, thanks again!
 
Actually you can also use AUTOIT which
is OPEN SOURCE and FREE and it can generate
.EXE files for you to launch from your app

I think the limitation of Autoit is that it
needs the app to be in Focus.. It cannot
send info to windows if the app is not in focus

Aprivate
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top