How to find window handle of object with focus (caret) in external application

Galaxea

New Member
Joined
Jul 20, 2018
Messages
34
Office Version
  1. 2021
Platform
  1. Windows
I am navigating an external application with UIAutomation, but this can only get me so far.
Some objects don't appear or are not accessible until the mouse clicks on them or they are TABBED to, and I am using SendKeys to TAB to them.

I now need to get the window handle of the element that has the edit caret.
I can't find any examples of this, well not any in anything like VBA.
Please point me to some examples. Thanks.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Is this query related to your other thread? I've been following that thread when I've had some time - I find I always learn something new from John and Jaafar's replies. I see from the past correspondence that you've come across/are aware of FindWindowEx - this is how you would normally go about getting the handle of an Edit control (classname 'Edit') in an external application. Does that not work in this scenario? You may have already be aware of it, but John_w has published in this site a very useful tool that enumerates through window handles with the EnumChildWindows API - link. Does this manage to capture the handle?
 
Upvote 0
Assuming the object has a hwnd , you could probably use GetCaretPos then WindowFromPoint.
You may need to first convert the caret position returned by GetCaretPos in the lpPoint parameter to screen coordinates with ClientToScreen before calling WindowFromPoint.

Also, if the object has no hwnd but the external application implements Active Accessibility, you can use the AccFocus method which retrieves the object that has the keyboard (caret). Once you have a pointer to the accessible object, you can for example, use the DoDefaultAction method to perform the object's default action. (if that is what you want)
 
Upvote 0

Forum statistics

Threads
1,216,084
Messages
6,128,722
Members
449,465
Latest member
TAKLAM

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top