Robin Scherbatsky
Robin Scherbatsky

Reputation: 61

Run-time error; Automation Error; Unspecified Error in VBA using UIAutomation

It was working before but I now I keep getting the error code "Automation Error; Unspecified Error" when i set the oElements on line 12 of the sub. Any thoughts?

Sub SavePath(ByVal strWindowID As String, ByVal strObjectName As String, ByVal strAutomationId As String, ByVal strLocalizeType As String, ByVal strValue As String)

Dim intElementCounter As Integer
Dim strTreeItem1 As String
Dim strTreeitem2 As String
Dim strTreeitem3 As String

strThiswbFileName = ActiveWorkbook.Name
strThiswbCaption = Application.Caption

Set oTW = oAutomation.ControlViewWalker
Set oCondition = oAutomation.CreatePropertyCondition(UIAutomationClient.UIA_NamePropertyId, strObjectName)
Set oElements = oAutomation.GetRootElement.FindAll(TreeScope_Descendants, oCondition)

For intElementCounter = 0 To oElements.length - 1
    If oElements.GetElement(intElementCounter).CurrentName = strObjectName Then
        If oElements.GetElement(intElementCounter).CurrentAutomationId = strAutomationId Then
            If oElements.GetElement(intElementCounter).CurrentLocalizedControlType = strLocalizeType Then
                Set oPatternValue = oElements.GetElement(intElementCounter).GetCurrentPattern(UIAutomationClient.UIA_ValuePatternId)
                oPatternValue.SetValue strValue
                Exit Sub
            End If
        End If
    End If
Next
End Sub

My references are

Upvotes: 0

Views: 1490

Answers (2)

Infrequent Coder
Infrequent Coder

Reputation: 99

Just dropping in to slightly expand on the answer provided by @MacroMarc. Microsoft documentation cautions using "Treescope_Descendants" from the root element, stating that it can cause a stack overflow. This is likely the technical reason that the children should be used in this case.

Upvotes: 0

MacroMarc
MacroMarc

Reputation: 3324

Don't use treescope_descendants from RootElement without care! I don't know why, but there can be error in the FindAll function, often when going through descendants of the root.

Let's assume you're looking for a 'Save As' dialog(and then the filepath textfield within).

Loop through rootelement.children, and look for the owner of the 'Save As' window. So if the owner is an Internet explorer browser, then check rootelement.children for those with UIA_Nameproperty like '*Internet Explorer'. Then search their descendants for the SaveAs box.

Alternatively, try adding a different condition when setting oElements which gets the ones with windowpattern:

Set oCondition = oAutomation.CreatePropertyCondition(UIAutomationClient.UIA_IsWindowPatternAvailablePropertyId, True)

You already are checking again for nameproperty in your first if block

Upvotes: 1

Related Questions