投稿‎ > ‎

Click Dialog Button in VBA

posted Mar 1, 2016, 9:09 PM by Zhang Wenxu   [ updated Mar 1, 2016, 9:10 PM ]
Public Function clickDialogButton(dlgTitle as String, caption as String) as Boolean
    Dim uiAuto as New UIAutomationClient.CUIAutomation
    Dim elmDialog as UIAutomationClient.IUIAutmationElement
    Dim j as Integer
    Dim cndButtonName as UIAutomationClient.IUIAutomaitonCondition
    Dim elmButton asUIAutomationClient.IUIAutomationElement
    Dim ptnAccButton as UIAutomationClient.IUIAutomationLegacyIAccessiblePattern
    Dim h as Long

    clickDialogButton = False
    h = FindWindow(vbNullString, dlgTitle)
    If h = 0 Then Exit Function
    Set elmDialog = uiAuto.ElementFromHandle(ByVal h)
    Set cndButtonName = uiAuto.CreatePropertyCondition(UIA_NamePropertyID, caption)
    Set elmButton = elmDialog.FindFirst(TreeScope_SubTree, cndButtonName)
    Set ptnAccButton = elmButton.GetCurrentPattern(UIA_LegacyIAccessiblePatternId)
    ptnAccButton.DoDefaultAction
    clickDialogButton = True
End Function
Comments