An Enthusiastic Programmer

UI Automation On Excel Macro

|

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).

Basic Concepts

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.

Component DLL Description
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.

AltRelationship 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.

Adding Reference

Create an Excel file and then open its VBA editor by striking [ALT]+[F11].

At the menu Tools -> References..., scroll down to find and refer UIAutomationClient component.

AltAdding UI AutomationClient reference

Creating CUIAutomation Object

 Dim oAutomation As New CUIAutomation

Searching Elements

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).

Scope and 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
Dim oAutomation As New CUIAutomation

' find first child element with classname
' oAutomation.GetRootElement is the top-level element
Dim MyElement1 As UIAutomationClient.IUIAutomationElement
Set MyElement1 = oAutomation.GetRootElement.FindFirst(TreeScope_Children, UiAutomation.CreatePropertyCondition(UIAutomationClient.UIA_ClassNamePropertyId, "class name"))

' find first descendant element with automation id
Dim MyElement2 As UIAutomationClient.IUIAutomationElement
Set MyElement2 = MyElement1.FindFirst(TreeScope_Descendants, UiAutomation.CreatePropertyCondition(UIAutomationClient.UIA_AutomationIdPropertyId, "automation id"))

' find first parent element with name
Dim MyElement3 As UIAutomationClient.IUIAutomationElement
Set MyElement3 = MyElement2.FindFirst(TreeScope_Parent, UiAutomation.CreatePropertyCondition(UIAutomationClient.UIA_NamePropertyId, "name"))

Find All elements

' Creating CUIAutomation element
Dim oAutomation As New CUIAutomation

' find all elements with "button" as their's classname
Dim MyElement4 As UIAutomationClient.IUIAutomationElementArray
Set MyElement4 = MyElement3.FindAll(TreeScope_Children, UiAutomation.CreatePropertyCondition(UIAutomationClient.UIA_ClassNamePropertyId, "button"))

Get Element’s unique identifier

' Get a unique identifier of an element
Dim uniqueIdentifier As Variant
 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 !!!

Focus Element

' make MyElement3 be focused on the screen window
MyElement3.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

' input text
Set oPattern = MyElement3.GetCurrentPattern(UIAutomationClient.UIA_LegacyIAccessiblePatternId)
oPattern.SetValue ("input text")

Click

' click a button
' btnElement is a button element
Set oPattern = btnElement.GetCurrentPattern(UIAutomationClient.UIA_InvokePatternId)
ePattern.Invoke

Conclusion

Above straightly illustrated the steps of creating an excel macro application with Microsoft UI Component. The following reviews might help you to understand better.

  • Adding UIAutomationClient in reference window
  • Searching elements through FindFirst or FindAll
  • Performing actions through Control Pattern

Comments