I have an application where I can find its text-boxes when they have focus with VBA UIAutomation via the GetFocusedElement method, which should indicate that they are detectable by UIAutomation, but I cannot find them with FindFirst or FindAll methods, as demonstrated in the following code:
Public Wnd As IUIAutomationElement
Public Element As IUIAutomationElement
Public H_wnd As Long, PropValue(30003 To 30005)
Declare PtrSafe Function GetForegroundWindow Lib "user32" () As Long
Sub GetFocused()
Tasks(MyApp).Activate
H_wnd = GetForegroundWindow
Set Element = UIauto.GetFocusedElement
For i = 30003 To 30005 'ControlType, ?, Name
PropValue(i) = Element.GetCurrentPropertyValue(i)
'works good
Debug.Print PropValue(i)
Next
End Sub
Sub FindElement()
Set Wnd = UIauto.ElementFromHandle(ByVal H_wnd)
For i = 30003 To 30005
'searching with the previously saved property values
Debug.Print Wnd.FindFirst(TreeScope_Descendants, _
UIauto.CreatePropertyCondition(i, PropValue(i))) Is Nothing
'prints True to all
Next
End Sub
The first Sub successfully finds the focused element, and prints its properties to the immediate pane. But the second Sub finds nothing, and prints True to all inquiries. That doesn't change if I replace TreeScope_Descendants with any of the other values of TreeScope.
So is there a possibility to find those elements without them being in focus? I'm trying to them by their names or properties, not by getting the focused control, because when I will use this code they will not be focused.