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
|
'### Constantes à adapter ###
Const FILE_OPEN As String = "c:\test unprotect.xls"
Const NB_ESSAI As Long = 3
'############################
Sub Launch()
Dim MotPasse As String
MotPasse = "faux mot de passe" 'dans l'exemple, le mot de passe est "zaza"
Call DemandePassword(FILE_OPEN, MotPasse)
End Sub
Sub DemandePassword(FileOpen As String, PassWord As String)
Dim WB As Workbook
Dim NbPasse&
Dim i&
Dim A$
Set WB = Workbooks.Open(FileOpen)
If WB.ProtectWindows Or WB.ProtectStructure Then
On Error Resume Next
WB.Unprotect (PassWord)
If Err.Number = 1004 Then
For i& = NB_ESSAI To 1 Step -1
Err.Clear
If i& = 1 Then
A$ = "Il ne vous reste qu'une tentative. "
Else
A$ = "Il vous reste " & i& & " tentatives. "
End If
PassWord = Application.InputBox( _
prompt:="Le mot de passe saisi n'est pas valide." & vbCrLf & vbCrLf & _
A$ & "Veuillez le saisir à nouveau.", _
Title:="Mot de passe incorrect.")
WB.Unprotect (PassWord)
If Err = 0 Then Exit For
Next i&
End If
If Err <> 0 Then Exit Sub
End If
End Sub |