There is a few intents of using IUIAutomation
with VBA
in this forum. But all of them relies on the fact that you know the name of the class to use it in FindWindowEx
. In the most popular one is:
Controlling IE11 "Do you want to Open/Save" dialogue window buttons in VBA
hWnd = FindWindowEx(hWnd, 0, "Frame Notification Bar", vbNullString)
The code in this website can help you to understand how PARENTS and CHILDRENS handlers are related http://www.vbaexpress.com/kb/getarticle.php?kb_id=52
And Autoit -> AutoIt Window Info) https://www.autoitscript.com/site/autoit/ allows you to click on a window and get the handler/class/name details.
It is very frustrating and hard to narrow down to the element we want to click. This code allows you to do that
Sub test2()
Dim h1, h2 As Long
Dim sWindowName As String
Dim AutomationObj As IUIAutomation
Dim WindowElement As IUIAutomationElement
Dim Button As IUIAutomationElement
Dim hWnd As LongPtr
sWindowName = vbNullString
Set AutomationObj = New CUIAutomation
h1 = FindWindow("#32770", "Internet Explorer")
Debug.Print h1
While h1 <> 0
h2 = FindWindowEx(h1, 0, vbNullString, sWindowName)
If h2 <> 0 Then
Set WindowElement = AutomationObj.ElementFromHandle(ByVal h2)
Dim iCnd As IUIAutomationCondition
Set iCnd = AutomationObj.CreatePropertyCondition(UIA_NamePropertyId, "Save")
Set Button = WindowElement.FindFirst(TreeScope_Subtree, iCnd)
Dim InvokePattern As IUIAutomationInvokePattern
If Button.CurrentName = "Save" Then
Set InvokePattern = Button.GetCurrentPattern(UIA_InvokePatternId)
InvokePattern.Invoke
h1 = 0
Else
h1 = h2
End If
Else
h1 = 0
End If
Debug.Print h2
Wend
End Sub
h1
and the FindWindow
allows you to narrow down to get the handle of the IE window (and the SAVE button)
Does anyone have a batter approach than this?.. Because here I'm just looping through the handlers under #32770 waiting to get the SAVE button. It does work, but I'm hopping it should be a systematic/efficient way to understand how to get the correct handler to use in IUIAutomation