J'ai lu cette discussion et je suis confronté au même problème, déverrouiller des macros pour les modifier puis les reverrouiller.
Pour faire des essais, j'ai repris ce code un peu plus haut:
Mais j'ai une erreur sur le Workbooks("Proteger_deproteger.xls"), "L'indice n'appartient pas à la sélection". Le fichier existe bel et bien, je ne comprend pas d'où ça vient.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 Sub TestProtect() ProtectVBProject Workbooks("Proteger_deproteger.xls"), "motdepasse" End Sub Sub TestUnprotect() UnprotectVBProject Workbooks("Proteger_deproteger.xls"), "motdepasse" End Sub Sub UnprotectVBProject(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 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 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
Une idée?
Partager