UI Automation On Excel Macro19 Jul 2021 | VBA
In this chapter, I am gonna illustrate how to use the Microsoft UI Automation component. Microsoft UI Automation is a new accessibility framework for Microsoft Windows, available on all operating systems that support Window Presentation Foundation(WPF).
It’s easy to build up robustness and assistive programs with Microsoft UI Automation. Several concepts need to be familiar before this article. Microsoft UI Automation contains four main components, as shown in the following table.
|Provider API||UIAutomationProvider.dll and UIAutomationTypes.dll||A set of interface definitions that are implemented by UI Automation providers, objects that provide information about UI elements and respond to programmatic input.|
|Client API||UIAutomationClient.dll and UIAutomationTypes.dll||A set of types for managed code that enables UI Automation client applications to obtain information about the UI and to send input to controls.|
|Core||UiAutomationCore.dll||The underlying code (sometimes called the UI Automation core) that handles communication between providers and clients.|
|ClientsideProviders||UIAutomationClientsideProviders.dll||A set of UI Automation providers for standard legacy controls. (WPF controls have native support for UI Automation.) This support is automatically available to client applications.|
The following dialogue represented the relationship between Automation Provider, Automation Client, and Automation Core.
Relationship Between Provider, Client And Core
Basically, for developers, there are two perspectives: to create support for custom controls (using the Provider API), and creating applications that use the UI Automation core to communicate with UI elements (using the Client API). For better understanding about Microsoft UI automation element tree structure, need to make an acquaintance with Control Patterns and Control Type.
AutomationElement objects expose common properties of the UI elements they represent. One of these properties is the control type, which defines its basic appearance and functionality as a single recognizable entity: for example, a button or check box.
In addition, elements expose control patterns that provide properties specific to their control types. Control patterns also expose methods that enable clients to get further information about the element and to provide input.
UI Automation Client also provides a mechanism that enables a client to gather information through events. You can register specific event notifications, and the element’s properties and control patterns will pass into their handlers when the event raise.
Next, I am going to lead you to create a simple macro excel application with the Microsoft UI Automation Client component step by step. I have wrote a snippet of Microsoft UI Automation’s usage at UI Automation Snippet Example, which may be assistive for you.
Create an Excel file and then open its VBA editor by striking
At the menu
References..., scroll down to find and refer
Adding UI AutomationClient reference
Creating CUIAutomation Object
DimoAutomation As NewCUIAutomation
UI Automation Client provides several available ways to find elements. You can use
inspecter.exe as an assistive software, which can inspect details of UI elements(such as: classname, automation id, and name).
Condition are required for searching elements.
Scope specifies the scope of variant operations in UI Automation tree.
Condition used in filtering when searching for elements in the UI Automation tree. Take a look at TreeScope enumeration and IUIAutomationCondition interface for more details.
Find first child or descendant element
' Creating CUIAutomation element DimoAutomation As NewCUIAutomation ' find first child element with classname ' oAutomation.GetRootElement is the top-level element DimMyElement1 AsUIAutomationClient.IUIAutomationElement SetMyElement1 = oAutomation.GetRootElement.FindFirst(TreeScope_Children, UiAutomation.CreatePropertyCondition(UIAutomationClient.UIA_ClassNamePropertyId, "class name")) ' find first descendant element with automation id DimMyElement2 AsUIAutomationClient.IUIAutomationElement SetMyElement2 = MyElement1.FindFirst(TreeScope_Descendants, UiAutomation.CreatePropertyCondition(UIAutomationClient.UIA_AutomationIdPropertyId, "automation id")) ' find first parent element with name DimMyElement3 AsUIAutomationClient.IUIAutomationElement SetMyElement3 = MyElement2.FindFirst(TreeScope_Parent, UiAutomation.CreatePropertyCondition(UIAutomationClient.UIA_NamePropertyId, "name"))
Find All elements
' Creating CUIAutomation element DimoAutomation As NewCUIAutomation ' find all elements with "button" as their's classname DimMyElement4 AsUIAutomationClient.IUIAutomationElementArray SetMyElement4 = MyElement3.FindAll(TreeScope_Children, UiAutomation.CreatePropertyCondition(UIAutomationClient.UIA_ClassNamePropertyId, "button"))
Get Element’s unique identifier
' Get a unique identifier of an element DimuniqueIdentifier AsVariant uniqueIdentifier = MyElement3.GetRuntimeId()
The identifier is only guaranteed to be unique to the UI of the desktop on which it was generated. Identifiers can be reused over time.
NOTE: Identifier may change in the future !!!
' make MyElement3 be focused on the screen windowMyElement3.SetFocus
Retrieving Control Pattern
Assumpting that you had known the difference between Control Type and Control Pattern, If not, take a look at the previous article. Control Pattern provides the client a way to manipulate ui elements(such as: click, input, or dropdown, etc…)
Each UI Elements have at least one pattern, Take a look at Control Pattern Identifiers for more details.
' input text SetoPattern = MyElement3.GetCurrentPattern(UIAutomationClient.UIA_LegacyIAccessiblePatternId) oPattern.SetValue ("input text")
' click a button ' btnElement is a button element SetoPattern = btnElement.GetCurrentPattern(UIAutomationClient.UIA_InvokePatternId) ePattern.Invoke
Above straightly illustrated the steps of creating an excel macro application with Microsoft UI Component. The following reviews might help you to understand better.
UIAutomationClientin reference window
- Searching elements through
- Performing actions through