Option Explicit
Private Type POINTAPI
x As Long
Y As Long
End Type
#If VBA7 Then
Private Declare PtrSafe Function GetCursorPos Lib "user32.dll" (lpPoint As POINTAPI) As Long
#If Win64 Then
Private Declare PtrSafe Function AccessibleObjectFromPoint Lib "Oleacc" (ByVal arg1 As LongPtr, ppacc As IAccessible, pvarChild As Variant) As Long
Private Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As LongPtr)
#Else
Private Declare PtrSafe Function AccessibleObjectFromPoint Lib "Oleacc" (ByVal lX As Long, ByVal lY As Long, ppacc As IAccessible, pvarChild As Variant) As Long
#End If
Private Declare PtrSafe Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare PtrSafe Function SetTimer Lib "user32" (ByVal hwnd As LongPtr, ByVal nIDEvent As LongPtr, ByVal uElapse As Long, ByVal lpTimerFunc As LongPtr) As LongPtr
Private Declare PtrSafe Function KillTimer Lib "user32" (ByVal hwnd As LongPtr, ByVal nIDEvent As LongPtr) As Long
#Else
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function AccessibleObjectFromPoint Lib "Oleacc" (ByVal lX As Long, ByVal lY As Long, ppacc As IAccessible, pvarChild As Variant) As Long
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Private Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
#End If
Private Const CHILDID_SELF = &H0&
Private Const S_OK As Long = &H0
Private bEnabeHook As Boolean
Private Sub Auto_Open()
If Not bEnabeHook Then
bEnabeHook = True
SetTimer Application.hwnd, 0, 0, AddressOf HookProc
End If
End Sub
Private Sub Auto_Close()
KillTimer Application.hwnd, 0
bEnabeHook = False
End Sub
Private Sub HookProc()
Dim oIA As IAccessible
Dim lResult As Long
Dim tMousePos As POINTAPI
GetCursorPos tMousePos
#If Win64 Then
Dim lngPtr As LongPtr
CopyMemory lngPtr, tMousePos, LenB(tMousePos)
lResult = AccessibleObjectFromPoint(lngPtr, oIA, 0)
#Else
lResult = AccessibleObjectFromPoint(tMousePos.x, tMousePos.Y, oIA, 0)
#End If
If lResult = S_OK Then
If InStr(1, oIA.accName(CHILDID_SELF), "Ôter la protection de la feuille", vbTextCompare) Or _
InStr(1, oIA.accName(CHILDID_SELF), "Unprotect Sheet", vbTextCompare) Then
If GetAsyncKeyState(VBA.vbKeyLButton) <> 0 Then
KillTimer Application.hwnd, 0
If MsgBox("This Page is Protected for a reason." & vbLf & _
"Do you have permission to unprotect this sheet ?", vbYesNo + vbExclamation) = vbYes Then
CommandBars.ExecuteMso ("SheetProtect")
End If
SetTimer Application.hwnd, 0, 0, AddressOf HookProc
End If
End If
End If
End Sub