Bonjour à tous,
Je possède un fichier avec plusieurs Userform. Lors de l'ouverture du fichier, je lance le code suivant sur Workbook.Open :
ThisWorkbook.VBProject.VBComponents("UserForm2").Activate
Sans ce petit bout de code mon fichier plante lors de l'ouverture du UserForm (ne me demandez pas pourquoi ça fait plus d'un an que je cherche ...).
Depuis j'ai rajouté du code et j'ai mis en place un mot de passe sur le projet VBA.
Problème : Avec le mot de passe, ce bout de code refuse de se lancer ... Il marque :
Erreur d'exécution '50289':
Impossible d'effectuer cette opération tant que le projet est protégé.
J'ai testé le code de Frédéric SIGONNEAU pour retiré le mot de passe du projet. Cela fonctionne sauf en le réactivant, car le code reste visible pour l'utilisateur.
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 41 42
| Sub UnprotectVBProject(WB As Workbook, ByVal Password As String)
Dim vbProj As Object
Set vbProj = WB.VBProject
Password = "mdp"
If vbProj.Protection <> 1 Then Exit Sub
Set Application.VBE.ActiveVBProject = vbProj
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
Password = "mdp"
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
End Sub
Sub Protect()
ProtectVBProject ThisWorkbook, "VBAProject"
DoEvents
End Sub
Sub Unprotect()
UnprotectVBProject ThisWorkbook, "VBAProject"
DoEvents
End Sub |
Ce que je souhaiterais faire c'est désactiver le mot de passe, lancer mon code et réactiver le mot de passe sans qu'après l'utilisateur n'est accès au projet VBA.
Des idées ? Merci par avance.
Partager