投稿‎ > ‎

Set Dialog Input Value in VBA

posted Mar 1, 2016, 9:26 PM by Zhang Wenxu
Public Function setDialogInputValue(dlgTitle as String, label As String, value As String) As Boolean
    Dim elmDialog As UIAutomationClient.IUIAutomationElement
    Dim cndEditControl As UIAutomationClient.IUIAutomationCondition
    Dim aryEditControl As UIAutomationClient.IUIAutomationElementArray
    Dim j As Integer
    Dim ptnVal As UIAutomationClient.IUIAutomationValuePattern
    Dim inputText As UIAutomationClient.IUIAutomationElement
    Dim uiAuto As New UIAutomationClient.CUIAutomation
    Dim h as Long
    
     h = FindWindow(vbNullString, dlgTitle)
    If h = 0 Then Exit Function
    Set elmDialog = uiAuto.ElementFromHandle(ByVal h)
    Set cndEditControl = uiAuto.CreatePropertyCondition(UIA_ControlTypePropertyId, UIA_EditControlTypeId)
    Do
        Set aryEditControl = elmDialog.FindAll(TreeScope_SubTree, cndEditControl)
        DoEvents
    Loop Until aryEditControl.Length > 1

    For j = 0 To aryEditControl.Lenght - 1
         DoEvents
         If LCase(aryEditControl.GetElement(j).CurrentName = LCase(label) Then
             Set inputText = aryEditControl.getElement(j)
             Set ptnVal = inputText.GetCurrentPattern(UIA_ValuePatternId)
             ptnVal.SetValue value
             Exit For
         End If
     Next
     SetDialogInputValue = True
End Function
Comments