Button Press Not Recognized (VBA UI Automation Library)

Viewed 29

Not all VBA users are aware that there is a library for UI automation which can be referenced. Please see this link: Microsoft Documentation

I have been in the process of automating the UI of a specific program but have encountered a slight error.

I am able to set all automation elements of the application successfully. These elements all happen to be buttons. However, when invoking the pattern which is meant to "click" the button, not every "click" is successful. In fact, if i call the invoke method three times per button, most of the "clicks" go through.

How can I be sure that each "click" is successful with only one method use per button? Per request, I have included sample code below:

Private Sub keypadPress(key As Byte)
    Dim keypadButtonPress As UIAutomationClient.IUIAutomationInvokePattern
    Dim keypadButton As UIAutomationClient.IUIAutomationElement
    Dim pCondition As UIAutomationClient.IUIAutomationCondition
    Dim patternID As Long
    Set pCondition = propertyCondition(cAuto, "Button" & key, xAutomation)
    Set keypadButton = keypad.FindFirst(TreeScope_Children, pCondition)
    patternID = UIAutomationClient.UIA_InvokePatternId
    Set keypadButtonPress = keypadButton.GetCurrentPattern(patternID)
    Debug.Print patternID
    keypadButtonPress.Invoke
End Sub

The key parameter simply is used to alternate the name of the button being pressed.

To expand on what unsuccessful means, the code executes, yet the buttons do not seem to be pressed. Nothing happens on the client side. When I invoke a button three times, it generally works, but sometimes it includes extra presses. For this reason, I cannot rely on three calls of the invoke method per button.

0 Answers
Related