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 43 44
| '=========================== 'Bill Manville, mpep
Const MON_CLASSEUR As String = "c:\toto.xls" 'à adapter
Const MOT_PASSE As String = "zaza" 'à adapter
Sub TestProtect()
Dim WB As Workbook
Set WB = Workbooks.Open(MON_CLASSEUR)
ProtectVBProject WB, MOT_PASSE
End Sub
Sub TestUnprotect()
Dim WB As Workbook
Set WB = Workbooks.Open(MON_CLASSEUR)
UnprotectVBProject WB, MOT_PASSE
DoEvents
With WB
'--- Ajout d'un module standard ---
.VBProject.VBComponents.Add 1
'... etc ...
'----------------------------------
End With
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
'=========================== |
Partager