Bonjour a tous,
Premierement, bravo pour le nouveau fofo, il est tres beau.
ma question porte sur un code que j'ai trouvé sur plusieurs fofo. ce code permets de proteger et deproteger les modules depuis une macro (activée par un bouton d'une feuille)
Le code de deverouillage marche tres bien (cela evite que ma macro plante avec le message " execution impossible si pas verouillé")
le deverouillage est immediat.
Par contre, mon soucis porte sur le code de protection.
Il fonctionne mais n'est efficace (apres enregistrement) qu'a la reouverture du classeur .
je m'explique : une fois la macro executée (deprotegé...fait sont taf....), avec un alt+f11, on a acces au code.
j'aimerais que la reprotection bloque l'ouverture des modules (comme quand on le fait a la main)
voici le code :
Sub TestUnprotect()
UnprotectVBProject Workbooks("zz-tps-gamme.xlsm"), "toto"
SendKeys "{NUMLOCK}"
End Sub
Sub UnprotectVBProject(WB As Workbook, ByVal Password As String)
Dim vbProj As Object
Set vbProj = WB.VBProject
'can't do it if already unlocked!
If vbProj.Protection <> 1 Then Exit Sub
Set Application.VBE.ActiveVBProject = vbProj
' now use lovely SendKeys to quote the project password
SendKeys Password & "~~"
Application.VBE.CommandBars(1).FindControl(ID:=2578, recursive:=True).Execute
End Sub
'************************************
Sub TestProtect()
ProtectVBProject Workbooks("zz-tps-gamme.xlsm"), "toto"
SendKeys "{NUMLOCK}"
End Sub
Sub ProtectVBProject(WB As Workbook, ByVal Password As String)
Dim vbProj As Object
Set vbProj = WB.VBProject
If vbProj.Protection = 1 Then Exit Sub
Set Application.VBE.ActiveVBProject = vbProj
SendKeys "+{TAB}{RIGHT}%V{+}{TAB}" & Password & "{TAB}" & _
Password & "~"
Application.VBE.CommandBars(1).FindControl(ID:=2578, recursive:=True).Execute
WB.Save
'SendKeys "{NUMLOCK}"
End Sub
Merci de votre aide