| VBA - UI Automation Project
sample code |
| Dim MyElement
As UIAutomationClient.IUIAutomationElement |
| Dim MyElement1
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 Test() |
| 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("BTB Portal 3.0 KA") |
|
| Set MyElement =
AppObj.FindFirst(TreeScope_Children, PropCondition(oAutomation,
eUIA_ClassNamePropertyId, "Frame Tab")) |
|
| Set MyElement1
= MyElement.FindFirst(TreeScope_Children, PropCondition(oAutomation,
eUIA_NamePropertyId, "BTB Portal 3.0 KA - Internet Explorer")) |
|
| Set MyElement =
MyElement1.FindFirst(TreeScope_Children, PropCondition(oAutomation,
eUIA_ClassNamePropertyId, "Shell DocObject View")) |
|
| Set MyElement1
= MyElement.FindFirst(TreeScope_Children, PropCondition(oAutomation,
eUIA_NamePropertyId, "http://ka.uhc.com/btb/")) |
|
| Set MyElement =
MyElement1.FindFirst(TreeScope_Children, PropCondition(oAutomation,
eUIA_NamePropertyId, "Silverlight Control")) |
|
| Set MyElement1
= MyElement.FindFirst(TreeScope_Children, PropCondition(oAutomation,
eUIA_AutomationIdPropertyId, "tabMain")) |
|
| Set MyElement =
MyElement1.FindFirst(TreeScope_Children, PropCondition(oAutomation,
eUIA_NamePropertyId, "Search")) |
|
| Set MyElement1
= MyElement.FindFirst(TreeScope_Children, PropCondition(oAutomation,
eUIA_AutomationIdPropertyId, "tabFindMain")) |
|
| Set MyElement =
MyElement1.FindFirst(TreeScope_Children, PropCondition(oAutomation,
eUIA_NamePropertyId, "Plan Search")) |
|
| Set MyElement1
= MyElement.FindFirst(TreeScope_Children, PropCondition(oAutomation,
eUIA_AutomationIdPropertyId, "txtPolicyNumber")) |
|
| Set oPattern =
MyElement1.GetCurrentPattern(UIA_LegacyIAccessiblePatternId) |
| |
| MsgBox "Member ID - " &
oPattern.CurrentValue |
|
|
| End Sub |
|
|
| Sub
ClickOnceInstaller() |
| 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 =
oAutomation.GetRootElement.FindFirst(TreeScope_Children,
PropCondition(oAutomation, eUIA_AutomationIdPropertyId,
"TrustManagerPromptUI")) |
|
| Set MyElement =
AppObj.FindFirst(TreeScope_Children, PropCondition(oAutomation,
eUIA_AutomationIdPropertyId, "tableLayoutPanelOuter")) |
|
| Set MyElement1
= MyElement.FindFirst(TreeScope_Children, PropCondition(oAutomation,
eUIA_AutomationIdPropertyId, "tableLayoutPanelButtons")) |
|
| Set MyElement =
MyElement1.FindFirst(TreeScope_Children, PropCondition(oAutomation,
eUIA_AutomationIdPropertyId, "btnCancel")) |
|
| Set
oInvokePattern =
MyElement.GetCurrentPattern(UIAutomationClient.UIA_InvokePatternId) |
| oInvokePattern.Invoke |
|
| Set MyElement1
= MyElement.FindFirst(TreeScope_Children, PropCondition(oAutomation,
eUIA_NamePropertyId, "http://ka.uhc.com/btb/")) |
|
| Set MyElement =
MyElement1.FindFirst(TreeScope_Children, PropCondition(oAutomation,
eUIA_NamePropertyId, "Silverlight Control")) |
|
| Set MyElement1
= MyElement.FindFirst(TreeScope_Children, PropCondition(oAutomation,
eUIA_AutomationIdPropertyId, "tabMain")) |
|
| Set MyElement =
MyElement1.FindFirst(TreeScope_Children, PropCondition(oAutomation,
eUIA_NamePropertyId, "Search")) |
|
| Set MyElement1
= MyElement.FindFirst(TreeScope_Children, PropCondition(oAutomation,
eUIA_AutomationIdPropertyId, "tabFindMain")) |
|
| Set MyElement =
MyElement1.FindFirst(TreeScope_Children, PropCondition(oAutomation,
eUIA_NamePropertyId, "Plan Search")) |
|
| Set MyElement1
= MyElement.FindFirst(TreeScope_Children, PropCondition(oAutomation,
eUIA_AutomationIdPropertyId, "txtPolicyNumber")) |
|
| Set oPattern =
MyElement1.GetCurrentPattern(UIA_LegacyIAccessiblePatternId) |
| |
| MsgBox "Member ID - " &
oPattern.CurrentValue |
|
|
| 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 |
|
| 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 |
Comments
Post a Comment