How to Find Unfocused UI Automation Elements in VBA
Question details
The user needs to locate a specific unfocused text box using UI Automation in VBA when standard search methods fail.

- Product
- VBA
- Device & OS
- not provided
- Scenario
- Automating application interactions where the target controls are not actively focused and reside within complex or nested window hierarchies.
- Observed behavior
- GetFocusedElement successfully detects the control when it is actively focused, but FindFirst and FindAll fail to return results from the foreground window when the element is unfocused.
Ensure you have a clear understanding of the target application's window hierarchy by inspecting it with accessibility tools like Inspect.exe or Spy++, and verify that the UIAutomationClient library is referenced in your VBA project.
Locate the Element via the Correct Child Window Handle
Use Windows API functions to navigate the hierarchy to the exact child window containing the element, then scope the UI Automation search specifically from that window.
When FindFirst fails from the main foreground window, it is usually due to the search scope being blocked by an intermediate child window. Bypassing this involves manually locating the correct intermediate window using the Windows API before initiating the UI Automation search.
At the top of your VBA module, declare the GetWindow and FindWindowExA API functions so you can interact with the Windows hierarchy directly.
Use FindWindowExA along with the parent window handle to locate the specific intermediate child window. You may need to pass the specific class name (e.g., 'Chrome_RenderWidgetHostHWND') to pinpoint the exact window hosting your controls.
Once you have the correct child window handle, call UIAuto.ElementFromHandle to create a new starting UI Automation element for your search.
Call FindFirst on the newly created element, using TreeScope_Descendants and the desired property condition (such as UIA_NamePropertyId) to successfully locate the unfocused text box.

Try WPS Office for Your VBA and Macro Automation Needs
If you frequently work with VBA macros, scripts, and complex automations, WPS Office provides a highly compatible, lightweight, and free alternative to Microsoft Office. It supports advanced macro execution while offering a familiar interface for seamless document management.
- 1. Download the installer: Visit the official WPS Office website and click the free download button to get the latest installation file.
- 2. Install WPS Office: Run the downloaded installer and follow the on-screen instructions to set up the software on your system.
- 3. Enable macros: Open your macro-enabled documents in WPS Office and follow the security prompts to enable VBA execution seamlessly.

Frequently Asked Questions
Why does GetFocusedElement work while FindFirst fails on the same element?
GetFocusedElement directly grabs the element currently holding system keyboard focus, bypassing the visual hierarchy tree. FindFirst relies on traversing the defined search scope (like descendants of the foreground window). If the target element resides in a nested child window that blocks the traversal scope, FindFirst will return no results.
How do I find the correct window class names for FindWindowEx?
You can use Microsoft accessibility and developer tools such as Inspect.exe, Accessibility Insights, or Spy++ to view the complete window hierarchy of the application. These tools will display the exact class names, handles, and titles needed for your FindWindowEx API calls.
Is UI Automation the only way to find unfocused text boxes in VBA?
For external or third-party applications, UI Automation combined with Windows API is typically required. However, if you are automating controls strictly within the Office application itself, you can use built-in object models like Application.CommandBars.FindControl or standard UserForm control references.




