par rvq35 » 13 Octobre 2015, 12:53
merci pour ta réponse mais je rencontre un problème
je vais bien dans le menu développeur puis visual basic et la je suis sur vba project "nom de mon fichier xls"
effectivement il demande le mot de passe.
sur le net on propose par exemple de créer un nouveau fichier et de copier cette macro mais quand je veux l'exécuter il me dit "erreur de compilation Sub ou fonction non définie"
cette macro est bien à faire dans un fichier excel annexe ?
sub TestProtect()
ProtectVBProject
Workbooks("Proteger_deproteger.xls"), "toto"
end sub
sub TestUnprotect()
UnprotectVBProject Workbooks("Proteger_deproteger.xls"), "toto"
end sub
sub
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 sub