0
votes

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

  • Visual Basic for Applications
  • Microsoft Excel 14.0 Object Library
  • OLE Automation
  • Microsoft ActiveX Data Objects 2.0 Library
  • Microsoft HTML Object Library
  • Microsoft Internet Controls
  • Microsoft Visual Basic for Applications Extensiblity 5.3
  • UIAutomationClient
1
Does this error go away intermittently? Maybe some problem with the dll loading, if it used to work... - MacroMarc

1 Answers

1
votes

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