Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

VBA - macro pour protéger et déprotéger le code

2 réponses
Avatar
jean plante
Bonjour

Je travaille avec Windows XP Professionnel et Excel 2003 SP1.

Est-ce qu'il est possible de créer une macro qui permet de protéger le code
VBA et de le déprotéger ?

Merci de votre aide.

Jean

2 réponses

Avatar
Papou
Bonjour
Oui, avec ce code de Chip Pearson :
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

'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:%78,
recursive:=True).Execute
End Sub

Sub ProtectVBProject(WB As Workbook, ByVal Password As String)
Dim vbProj As Object

Set vbProj = WB.VBProject

'can't do it if already locked!
If vbProj.Protection = 1 Then Exit Sub

Set Application.VBE.ActiveVBProject = vbProj

' now use lovely SendKeys to set the project password
SendKeys "+{TAB}{RIGHT}%V{+}{TAB}" & Password & "{TAB}" & _
Password & "~"

Application.VBE.CommandBars(1).FindControl(ID:%78,
recursive:=True).Execute

WB.Save
End Sub

Cordialement
Pascal

"jean plante" a écrit dans le message de
news:wvRUc.123276$
Bonjour

Je travaille avec Windows XP Professionnel et Excel 2003 SP1.

Est-ce qu'il est possible de créer une macro qui permet de protéger le
code

VBA et de le déprotéger ?

Merci de votre aide.

Jean




Avatar
jean plante
Bonjour Papou

Je te remercie de la réponse.


Jean

"Papou" a écrit dans le message de
news:
Bonjour
Oui, avec ce code de Chip Pearson :
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

'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:%78,
recursive:=True).Execute
End Sub

Sub ProtectVBProject(WB As Workbook, ByVal Password As String)
Dim vbProj As Object

Set vbProj = WB.VBProject

'can't do it if already locked!
If vbProj.Protection = 1 Then Exit Sub

Set Application.VBE.ActiveVBProject = vbProj

' now use lovely SendKeys to set the project password
SendKeys "+{TAB}{RIGHT}%V{+}{TAB}" & Password & "{TAB}" & _
Password & "~"

Application.VBE.CommandBars(1).FindControl(ID:%78,
recursive:=True).Execute

WB.Save
End Sub

Cordialement
Pascal

"jean plante" a écrit dans le message de
news:wvRUc.123276$
Bonjour

Je travaille avec Windows XP Professionnel et Excel 2003 SP1.

Est-ce qu'il est possible de créer une macro qui permet de protéger le
code

VBA et de le déprotéger ?

Merci de votre aide.

Jean