Protéger-déprotéger un classeur par VBA
Pour prépare un nouveau classeur dans lequel une macro corrige la macro défectueuse du premier classeur. Pour faire sauter par VBA la protection du premier projet utilise ceci :
sub TestProtect()
ProtectVBProject
Workbooks("Proteger_deproteger.xls"), "toto"
end subsub TestUnprotect()
UnprotectVBProject Workbooks("Proteger_deproteger.xls"), "toto"
end subsub UnprotectVBProject(WB As Workbook, ByVal Password As String) Dim vbProj As Object Set vbProj = WB.VBProject 'Teste si le classeur est déjà déprotégé If vbProj.Protection <> 1 Then exit sub Set Application.VBE.ActiveVBProject = vbProj 'uilise les sendkeys pourmasquer le mot de passe du projet SendKeys Password & "~~" Application.VBE.CommandBars(1).FindControl(ID:\=2578, recursive:=True).Execute end sub
sub ProtectVBProject(WB As Workbook, ByVal Password As String)
Dim vbProj As Object
Set vbProj = WB.VBProject
'teste si le projet est déjà verrouillé
If vbProj.Protection = 1 Then exit sub
Set Application.VBE.ActiveVBProject = vbProj
'utilise les sendKeys pour mettre le mot de passe
SendKeys "+{TAB}{RIGHT}%V{+}{TAB}" & Password & "{TAB}" &
Password & "~"
Application.VBE.CommandBars(1).FindControl(ID:\=2578, recursive:=True).Execute
WB.Save
end subAuteur : Bill Manville
Mots clefs associés à cette page : fichier, classeur, file, xls, macro, passer, mot, de, protéger, protection, vba
- Vous devez vous identifier ou créer un compte pour écrire des commentaires
