【问题标题】:VBA UIAutomation run time error :- "An event was unable to invoke any of the subscribers"VBA UIAutomation 运行时错误:-“事件无法调用任何订阅者”
【发布时间】:2021-09-22 19:00:45
【问题描述】:

代码的目的很简单。 找到发票的“窗口” -> 从 excel 中输入装载编号 -> 点击打印按钮

代码没有错误,但在代码行(如下所述)“调用”期间代码停止,几秒钟后,它显示错误

事件无法调用任何订阅者

如果我自己选择按钮,客户端应用程序中没有错误或问题。

Set oInvokePattern = MyElement1.GetCurrentPattern(UIAutomationClient.UIA_InvokePatternId)

oInvokePattern.Invoke

可以做什么?它将大大减轻我手动复制粘贴的工作。

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

【问题讨论】:

  • 对于未来的用户:只需添加睡眠就可以解决我的问题。但很多时候我仍然得到这个错误。

标签: excel vba automation ui-automation


【解决方案1】:

这段代码怎么样?

MyElement1.SetFocus

SendKeys "{Enter}"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多