Public Sub verifySecu()
Dim c As New CUIAutomation, root As IUIAutomationElement, oExcel As IUIAutomationElement
Dim oSecurity As IUIAutomationElement, oCheckbox As IUIAutomationElement, oCondition As IUIAutomationCondition, res, i
Dim allCB As IUIAutomationElementArray, oBtOK As IUIAutomationElement
On Error Resume Next
Set root = c.GetRootElement
Set oCondition = c.CreatePropertyCondition(UIAutomationClient.UIA_NamePropertyId, "Excel") 'fenêtre nouvelle instance
Set oExcel = root.FindFirst(TreeScope_Children, oCondition)
Set oCondition = c.CreatePropertyCondition(UIAutomationClient.UIA_NamePropertyId, "Centre de gestion de la confidentialité")
Set oSecurity = oExcel.FindFirst(TreeScope_Descendants, oCondition)
Set oCondition = c.CreatePropertyCondition(UIAutomationClient.UIA_NamePropertyId, "OK") ' bouton OK
Set oBtOK = oSecurity.FindFirst(TreeScope_Descendants, oCondition)
Set oCondition = c.CreatePropertyCondition(UIAutomationClient.UIA_ControlTypePropertyId, 50002) 'NetUICheckbox case à cocher
Set allCB = oSecurity.FindAll(TreeScope_Descendants, oCondition)
For i = 0 To allCB.Length - 1
Set oCheckbox = allCB.GetElement(i) ' la dernière CB est la bonne normalement
Debug.Print oCheckbox.CurrentName
Next i ' Récupérer le TogglePattern
Dim togglePattern As IUIAutomationTogglePattern
Set togglePattern = oCheckbox.GetCurrentPattern(UIA_TogglePatternId)
' Lire l’état actuel
Dim currentState As Integer
currentState = togglePattern.CurrentToggleState
Select Case currentState
Case ToggleState_Off
Debug.Print "Checkbox est décochée. On va la cocher."
togglePattern.Toggle ' Devient cochée
Case ToggleState_On
Debug.Print "Checkbox est cochée"
Case ToggleState_Indeterminate
Debug.Print "Checkbox est dans un état indéterminé."
End Select
Dim clicBtnOK As IUIAutomationInvokePattern
Set clicBtnOK = oBtOK.GetCurrentPattern(UIA_InvokePatternId)
clicBtnOK.Invoke ' cliquer sur le bouton OK
End Sub