Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA-自动化用户界面_Excel_Vba_Automation - Fatal编程技术网

Excel VBA-自动化用户界面

Excel VBA-自动化用户界面,excel,vba,automation,Excel,Vba,Automation,使用UI自动化库在VBA(Excel)中编写脚本时遇到问题。 困难在于在使用Invoke调用超链接后,另一个列表框窗口打开,程序停止崩溃时停止脚本 问题是,上述呼吁是否有效 是否可以使用UI自动化库进行操作 将MyElement设置为UIAutomationClient.IUIAutomationElement Dim MyElement1作为UIAutomationClient.IUIAutomationElement Dim MyElement2作为UIAutomationClient.I

使用UI自动化库在VBA(Excel)中编写脚本时遇到问题。 困难在于在使用Invoke调用超链接后,另一个列表框窗口打开,程序停止崩溃时停止脚本

问题是,上述呼吁是否有效 是否可以使用UI自动化库进行操作

将MyElement设置为UIAutomationClient.IUIAutomationElement
Dim MyElement1作为UIAutomationClient.IUIAutomationElement
Dim MyElement2作为UIAutomationClient.IUIAutomationElement
Dim MyElement3作为UIAutomationClient.IUIAutomationElement
Dim MyElement4作为UIAutomationClient.IUIAutomationElement
Dim MyElementz作为UIAutomationClient.IUIAutomationElement
作为UIAutomationClient.IUIAutomationElement的暗髓鞘
'Private Declare Sub Sleep Lib“kernel32”(ByVal的长度为毫秒)
公共枚举条件
eUIA_NamePropertyId
eUIA_自动属性ID
eUIA_ClassNamePropertyId
eUIA_本地化控制类型属性ID
结束枚举
子测试2()
作为UIAutomationClient.IUIAutomationElement的Dim AppObj
将oInvokePattern设置为UIAutomationClient.IUIAutomationInvokePattern
Dim oAutomation作为UI自动化API的新CUI自动化\
Dim oPattern作为UIAutomationClient.IUIAutomationLegacyAccessiblePattern
Set AppObj=walkabledelements(“xxx”)
设置MyElement=AppObj.FindFirst(TreeScope_子对象,PropCondition(oAutomation,eUIA_AutomationIdPropertyId,“Frm1”))
Set MyElement1=MyElement.FindFirst(TreeScope_子对象,PropCondition(oAutomation,eUIA_AutomationdPropertyId,“Frm2”))
Set MyElement2=MyElement1.FindFirst(TreeScope\u子对象,PropCondition(oAutomation,eUIA\u AutomationIdPropertyId,“tc1”))
Set MyElement3=MyElement2.FindFirst(TreeScope\u子对象,PropCondition(oAutomation,eUIA\u AutomationIdPropertyId,“tp1”))
Set MyElementz=MyElement3.FindFirst(TreeScope_子对象,PropCondition(oAutomation,eUIA_AutomationIdPropertyId,“llJ”))
Set MyElementy=MyElementz.FindFirst(TreeScope_Children,PropCondition(oAutomation,eUIA_NamePropertyId,“Jed”))
设置oInvokePattern=MyElementy.GetCurrentPattern(UIAutomationClient.UIA\u InvokePatternId)
oInvokePattern。调用“崩溃!!!!”!!!!
端接头
函数PropCondition(UiAutomation作为CUIAutomation,Prop作为条件,需求作为字符串)作为UIAutomationClient.IUIAutomationCondition
选择案例道具
案例0
Set-PropCondition=UiAutomation.CreatePropertyCondition(UIAutomationClient.UIA_-NamePropertyId,需求)
案例1
Set-PropCondition=UiAutomation.CreatePropertyCondition(UIAutomationClient.UIA\U AutomationPropertyID,要求)
案例2
Set-PropCondition=UiAutomation.CreatePropertyCondition(UIAutomationClient.UIA_ClassNamePropertyId,需求)
案例3
Set-PropCondition=UiAutomation.CreatePropertyCondition(UIAutomationClient.UIA\u LocalizedControlTypePropertyId,要求)
结束选择
端函数
作为UIAutomationClient.IUIAutomationeElement的函数WalkEnabledElements(strWindowName作为字符串)
Dim oAutomation作为一种新的自动化
Dim条件1作为UIAutomationClient.IUIAutomationCondition
Dim条件2作为UIAutomationClient.IUIAutomationCondition
Dim walker作为UIAutomationClient.IUIAutomationTreeWalker
作为UIAutomationClient.IUIAutomationElement的Dim元素
设置walker=oAutomation.ControlViewWalker
Set元素=walker.GetFirstChildElement(oAutomation.GetRootElement)
做而不做元素什么都不是
Debug.Print element.CurrentName
如果InStr(1,element.CurrentName,strWindowName)>0,则
设置WalkEnabledElements=element
退出功能
如果结束
Set element=walker.GetNextSiblingElement(元素)
环
端函数
函数GetElement(ElementList作为UIAutomationClient.IUIAutomationeElement)
出错时继续下一步
Dim oAutomation作为一种新的自动化
Dim walker作为UIAutomationClient.IUIAutomationTreeWalker
设置walker=oAutomation.ControlViewWalker
作为UIAutomationClient.IUIAutomationElementArray的Dim元素1
作为UIAutomationClient.IUIAutomationElement的Dim元素2
Dim childtree作为UIAutomationClient.TreeScope
Debug.Print elementalist.CurrentName
Dim条件1作为UIAutomationClient.IUIAutomationCondition
Set condition1=oAutomation.CreateTrueCondition
Set element1=elementList.FindAll(TreeScope_子对象,条件1)
多芬特
如果元素1.长度为0,则
Set element2=elementList.FindFirst(TreeScope_子项,条件1)
如果结束
不做就去做元素2什么都不是
Dim oPattern作为UIAutomationClient.IUIAutomationLegacyAccessiblePattern
Set oPattern=element2.GetCurrentPattern(UIA_LegacyIAccessiblePatternId)
Debug.Print element2.CurrentName&“|”&oPattern.CurrentValue&“|”&element2.cacheUnstrolElement
如果oPattern.CurrentName=“通知”,则
设置MyElement=element2
退出功能
如果结束
Debug.Print element2.CurrentClassName
调试.打印元素2.CurrentAutomationId
GetElement元素2
调试.打印元素2.CurrentName
如果不是MyElement,则退出函数
Set element2=walker.GetNextSiblingElement(element2)
环
端函数
函数GetElement1(ElementList作为UIAutomationClient.IUIAutomationeElement)
出错时继续下一步
Dim oAutomation作为一种新的自动化
Dim walker作为UIAutomationClient.IUIAutomationTreeWalker
设置walker=oAutomation.ControlViewWalker
作为UIAutomationClient.IUIAutomationElementArray的Dim元素1
作为UIAutomationClient.IUIAutomationElement的Dim元素2
Dim childtree作为UIAutomationClient.TreeScope
Debug.Print elementalist.CurrentName
Dim条件1作为UIAutomationClient.IUIAutomationCondition
Set condition1=oAutomation.CreateTrueCondition
Set element1=elementList.FindAll(TreeScope_子对象,条件1)
多芬特
如果元素1.Leng
Dim MyElement As UIAutomationClient.IUIAutomationElement
Dim MyElement1 As UIAutomationClient.IUIAutomationElement
Dim MyElement2 As UIAutomationClient.IUIAutomationElement
Dim MyElement3 As UIAutomationClient.IUIAutomationElement
Dim MyElement4 As UIAutomationClient.IUIAutomationElement
Dim MyElementz As UIAutomationClient.IUIAutomationElement
Dim MyElementy As UIAutomationClient.IUIAutomationElement

'Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Enum oConditions
   eUIA_NamePropertyId
   eUIA_AutomationIdPropertyId
   eUIA_ClassNamePropertyId
   eUIA_LocalizedControlTypePropertyId
End Enum

Sub Test2()
Dim AppObj As UIAutomationClient.IUIAutomationElement
Dim oInvokePattern As UIAutomationClient.IUIAutomationInvokePattern
Dim oAutomation As New CUIAutomation ' the UI Automation API\
Dim oPattern As UIAutomationClient.IUIAutomationLegacyIAccessiblePattern


Set AppObj = WalkEnabledElements("xxx")
Set MyElement = AppObj.FindFirst(TreeScope_Children, PropCondition(oAutomation, eUIA_AutomationIdPropertyId, "Frm1"))
Set MyElement1 = MyElement.FindFirst(TreeScope_Children, PropCondition(oAutomation, eUIA_AutomationIdPropertyId, "Frm2"))
Set MyElement2 = MyElement1.FindFirst(TreeScope_Children, PropCondition(oAutomation, eUIA_AutomationIdPropertyId, "tc1"))
Set MyElement3 = MyElement2.FindFirst(TreeScope_Children, PropCondition(oAutomation, eUIA_AutomationIdPropertyId, "tp1"))



Set MyElementz = MyElement3.FindFirst(TreeScope_Children, PropCondition(oAutomation, eUIA_AutomationIdPropertyId, "llJ"))
Set MyElementy = MyElementz.FindFirst(TreeScope_Children, PropCondition(oAutomation, eUIA_NamePropertyId, "Jed"))
Set oInvokePattern = MyElementy.GetCurrentPattern(UIAutomationClient.UIA_InvokePatternId)
oInvokePattern.Invoke  ' Crash !!!!




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

Function GetElement(elementalist As UIAutomationClient.IUIAutomationElement)
On Error Resume Next
Dim oAutomation As New CUIAutomation
    Dim walker As UIAutomationClient.IUIAutomationTreeWalker

    Set walker = oAutomation.ControlViewWalker
    Dim element1 As UIAutomationClient.IUIAutomationElementArray
    Dim element2 As UIAutomationClient.IUIAutomationElement

    Dim childtree As UIAutomationClient.TreeScope
    Debug.Print elementalist.CurrentName
    Dim condition1 As UIAutomationClient.IUIAutomationCondition
    Set condition1 = oAutomation.CreateTrueCondition
    Set element1 = elementalist.FindAll(TreeScope_Children, condition1)
    DoEvents
    If element1.Length <> 0 Then
        Set element2 = elementalist.FindFirst(TreeScope_Children, condition1)
    End If

    Do While Not element2 Is Nothing
        Dim oPattern As UIAutomationClient.IUIAutomationLegacyIAccessiblePattern
        Set oPattern = element2.GetCurrentPattern(UIA_LegacyIAccessiblePatternId)

        Debug.Print element2.CurrentName & "|" & oPattern.CurrentValue & "|" & element2.CachedIsControlElement
        

            If oPattern.CurrentName = "Notification" Then
                Set MyElement = element2
                Exit Function
            End If

        Debug.Print element2.CurrentClassName
        Debug.Print element2.CurrentAutomationId

        GetElement element2
        Debug.Print element2.CurrentName
        If Not MyElement Is Nothing Then Exit Function
        Set element2 = walker.GetNextSiblingElement(element2)
    Loop

End Function

Function GetElement1(elementalist As UIAutomationClient.IUIAutomationElement)
On Error Resume Next
Dim oAutomation As New CUIAutomation
    Dim walker As UIAutomationClient.IUIAutomationTreeWalker

    Set walker = oAutomation.ControlViewWalker
    Dim element1 As UIAutomationClient.IUIAutomationElementArray
    Dim element2 As UIAutomationClient.IUIAutomationElement

    Dim childtree As UIAutomationClient.TreeScope
    Debug.Print elementalist.CurrentName
    Dim condition1 As UIAutomationClient.IUIAutomationCondition
    Set condition1 = oAutomation.CreateTrueCondition
    Set element1 = elementalist.FindAll(TreeScope_Children, condition1)
    DoEvents
    If element1.Length <> 0 Then
        Set element2 = elementalist.FindFirst(TreeScope_Children, condition1)
    End If

    Do While Not element2 Is Nothing
        Dim oPattern As UIAutomationClient.IUIAutomationLegacyIAccessiblePattern
        Set oPattern = element2.GetCurrentPattern(UIA_LegacyIAccessiblePatternId)

        Debug.Print element2.CurrentName & "|" & oPattern.CurrentValue
            If element2.CurrentName = "Save" Then
                Set MyElement = element2
                Exit Function
            End If

        Debug.Print element2.CurrentClassName

        Debug.Print element2.CurrentAutomationId

        GetElement element2
        Debug.Print element2.CurrentName
        If Not MyElement Is Nothing Then Exit Function
        Set element2 = walker.GetNextSiblingElement(element2)
    Loop

End Function

Function AddReference() As Boolean
    Dim VBAEditor As VBIDE.VBE
    Dim vbProj As VBIDE.VBProject
    Dim chkRef As VBIDE.Reference
    Set VBAEditor = Application.VBE
    Set vbProj = ThisWorkbook.VBProject

    For Each chkRef In vbProj.References
        If chkRef.Name Like "*IBM PCOMM 4.01*" Then
        GoTo Flush
        End If
    Next
    On Error GoTo Hell:
    vbProj.References.AddFromFile Environ("systemroot") & "\system32\uiautomationcore.dll"
   
Hell:
    If Err.Number = 48 Then
        AddReference = False
    ElseIf Err.Number = 0 Then
        AddReference = True
    End If
Flush:
    Set vbProj = Nothing
    Set VBAEditor = Nothing
End Function