1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
| '---------------------------------------------------------------------------------------
' Module : Signature
' Author : Oliv
' Date : 01/11/2017
' Purpose : insert SIGNATURE in OUTLOOK 2016
'---------------------------------------------------------------------------------------
'##############Please add reference ###############
' UIAutomationClient
'##################################################
Option Explicit
Dim oApp
'Declare UIAutomationClient variable
Dim uiAuto As UIAutomationClient.CUIAutomation
Dim elmRibbon As UIAutomationClient.IUIAutomationElement
Dim elmRibbonTab As UIAutomationClient.IUIAutomationElement
Dim cndProperty As UIAutomationClient.IUIAutomationCondition
Dim aryRibbonTab As UIAutomationClient.IUIAutomationElementArray
Dim ptnAcc As UIAutomationClient.IUIAutomationLegacyIAccessiblePattern
Dim accRibbon As Office.IAccessible
Dim i As Long
'Declare sleep
#If VBA7 Then
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr) 'For 64 Bit Systems
#Else
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 'For 32 Bit Systems
#End If
'Declare mouse events
Public Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4
Public Const MOUSEEVENTF_RIGHTDOWN As Long = &H8
Public Const MOUSEEVENTF_RIGHTUP As Long = &H10
Sub Insert_Signature()
'---------------------------------------------------------------------------------------
' Procedure : Insert_Signature
' Author : Oliv
' Date : 02/11/2017
' parameter : replace in line [Call ClicSequence(Array("Une Signature", "Oliv"))]
' "Une Signature" = Label of Signature MenuItem in the ribbon
' "Oliv" = the signature
'---------------------------------------------------------------------------------------
'
On Error Resume Next
If UCase(Application) = "OUTLOOK" Then
Set oApp = Application
Else
Set oApp = CreateObject("outlook.application")
End If
Set uiAuto = New UIAutomationClient.CUIAutomation
Set accRibbon = oApp.ActiveInspector.CommandBars("Ribbon")
If accRibbon Is Nothing Then Exit Sub
Set elmRibbon = uiAuto.ElementFromIAccessible(accRibbon, 0)
If SelectRibbonTab("Message") Then
'Dans la version Française le nom du MenuItem SIGNATURE est différent entre OFFICE 2010 et 2016
If Val(oApp.Version) = 14 Then
Call ClicSequence(Array("Signature", "Oliv"))
Else
Call ClicSequence(Array("Une Signature", "Oliv"))
End If
End If
End Sub
Private Function SelectRibbonTab(NAME) As Boolean
SelectRibbonTab = False
Set cndProperty = uiAuto.CreatePropertyCondition(UIA_ClassNamePropertyId, "NetUIRibbonTab")
Set aryRibbonTab = elmRibbon.FindAll(TreeScope_Subtree, cndProperty)
For i = 0 To aryRibbonTab.Length - 1
Set elmRibbonTab = aryRibbonTab.GetElement(i)
If Not elmRibbonTab Is Nothing Then
If elmRibbonTab.CurrentControlType = UIA_TabItemControlTypeId And StrComp(elmRibbonTab.CurrentName, NAME, vbTextCompare) = 0 Then
Set ptnAcc = elmRibbonTab.GetCurrentPattern(UIA_LegacyIAccessiblePatternId)
ptnAcc.DoDefaultAction
DoEvents
Exit For
End If
End If
Next
If Not ptnAcc Is Nothing Then
' DoEvents
Sleep 50
SelectRibbonTab = True
End If
End Function
Private Sub ClicSequence(ByVal SeqName As Variant)
Dim sequence As Variant, truc
sequence = Array(Array(SeqName(0), "NetUIAnchor"), Array(SeqName(1), "NetUITWBtnCheckMenuItem"))
'"NetUIGalleryButton"))
For Each truc In sequence
Set cndProperty = uiAuto.CreatePropertyCondition(UIA_ClassNamePropertyId, truc(1))
If truc(0) = SeqName(1) Then
Set cndProperty = uiAuto.CreatePropertyCondition(UIAutomationClient.UIA_PropertyIds.UIA_IsControlElementPropertyId, True)
End If
Set aryRibbonTab = elmRibbon.FindAll(TreeScope_Subtree, cndProperty)
For i = 0 To aryRibbonTab.Length - 1
Debug.Print aryRibbonTab.GetElement(i).CurrentName
If StrComp(aryRibbonTab.GetElement(i).CurrentName, truc(0), vbTextCompare) = 0 Then
Set elmRibbonTab = aryRibbonTab.GetElement(i)
Exit For
End If
Next
If elmRibbonTab Is Nothing Then Exit Sub
Set ptnAcc = elmRibbonTab.GetCurrentPattern(UIA_LegacyIAccessiblePatternId)
'Debug.Print vbTab & ptnAcc.CurrentName
Dim pt As UIAutomationClient.tagPOINT
If truc(0) = SeqName(1) Then
elmRibbonTab.GetClickablePoint pt
Clickpoint pt.x, pt.y
Else
ptnAcc.DoDefaultAction
End If
Set elmRibbonTab = Nothing
'DoEvents
Sleep 400
Next truc
End Sub
Private Sub Clickpoint(x, y)
SetCursorPos x, y
Sleep 50
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
End Sub |