purpose of the code is simple. find "window" of invoice -> enter load number from excel -> click print button
Code is error free, but at the line (mentioned below) the code halts during "invoke" and after few seconds, it show the error
An event was unable to invoke any of the subscribers
if I select the button myself, there is no error or issue in the client application.
Set oInvokePattern = MyElement1.GetCurrentPattern(UIAutomationClient.UIA_InvokePatternId)
oInvokePattern.Invoke
what can be done? it will ease my manual work of copy paste by ton.
Dim MyElement As UIAutomationClient.IUIAutomationElement
Dim MyElement1 As UIAutomationClient.IUIAutomationElement
Public Enum oConditions
eUIA_NamePropertyId
eUIA_AutomationIdPropertyId
eUIA_ClassNamePropertyId
eUIA_LocalizedControlTypePropertyId
End Enum
Sub From22_printing()
Dim AppObj As UIAutomationClient.IUIAutomationElement
Dim oInvokePattern As UIAutomationClient.IUIAutomationInvokePattern
Dim oAutomation As New CUIAutomation ' the UI Automation API\
Dim oPattern As UIAutomationClient.IUIAutomationLegacyIAccessiblePattern
Dim FormPrint As Worksheet
Dim Pcount As Long
Set FormPrint = ThisWorkbook.Sheets("Form22 Print")
Pcount = Application.WorksheetFunction.CountA(FormPrint.Columns(1))
Set AppObj = WalkEnabledElements("Invoice Printing")
If AppObj Is Nothing Then
MsgBox "Form22 Not Open"
End
End If
For NOP = 2 To Pcount
'Find Load Number
Set MyElement = AppObj.FindFirst(TreeScope_Children, PropCondition(oAutomation, eUIA_AutomationIdPropertyId, "txtloadno"))
Set oPattern = MyElement.GetCurrentPattern(UIA_LegacyIAccessiblePatternId)
oPattern.SetValue (FormPrint.Cells(NOP, 1).Value)
Application.Wait (Now + TimeValue("0:00:02"))
'select Print
Set MyElement1 = AppObj.FindFirst(TreeScope_Children, PropCondition(oAutomation, eUIA_AutomationIdPropertyId, "btnprint"))
Set oInvokePattern = MyElement1.GetCurrentPattern(UIAutomationClient.UIA_InvokePatternId)
oInvokePattern.Invoke
Application.Wait (Now + TimeValue("0:00:10"))
Next NOP
End Sub
Function PropCondition(UiAutomation As CUIAutomation, Prop As oConditions, Requirement As String) As UIAutomationClient.IUIAutomationCondition
Select Case Prop
Case 0
Set PropCondition = UiAutomation.CreatePropertyCondition(UIAutomationClient.UIA_NamePropertyId, Requirement)
Case 1
Set PropCondition = UiAutomation.CreatePropertyCondition(UIAutomationClient.UIA_AutomationIdPropertyId, Requirement)
Case 2
Set PropCondition = UiAutomation.CreatePropertyCondition(UIAutomationClient.UIA_ClassNamePropertyId, Requirement)
Case 3
Set PropCondition = UiAutomation.CreatePropertyCondition(UIAutomationClient.UIA_LocalizedControlTypePropertyId, Requirement)
End Select
End Function
Function WalkEnabledElements(strWindowName As String) As UIAutomationClient.IUIAutomationElement
Dim oAutomation As New CUIAutomation
Dim condition1 As UIAutomationClient.IUIAutomationCondition
Dim condition2 As UIAutomationClient.IUIAutomationCondition
Dim walker As UIAutomationClient.IUIAutomationTreeWalker
Dim element As UIAutomationClient.IUIAutomationElement
Set walker = oAutomation.ControlViewWalker
Set element = walker.GetFirstChildElement(oAutomation.GetRootElement)
Do While Not element Is Nothing
Debug.Print element.CurrentName
If InStr(1, element.CurrentName, strWindowName) > 0 Then
Set WalkEnabledElements = element
Exit Function
End If
Set element = walker.GetNextSiblingElement(element)
Loop
End Function
How about this code?
MyElement1.SetFocus
SendKeys "{Enter}"