protéger macro dans fichier

  • Initiateur de la discussion Initiateur de la discussion patrick
  • Date de début Date de début

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

P

patrick

Guest
Bonjour,

Dans la macro suivante je recopie un module dans un classeur que je crée.
Quel est le code pour que la macro du classeur soit protégée par mot de passe ?

Dim S As String, Wbk As Workbook
Set Wbk = Workbooks("Perso.xls")
With Wbk.VBProject.VBComponents("Module7").CodeModule
S = .Lines(1, .CountOfLines)
End With

ActiveWorkbook.VBProject.VBComponents.Add 1
With ActiveWorkbook.VBProject.VBComponents("Module1").CodeModule
.AddFromString S
End With

Merci au forum
 
bonjour patrick ,

Oui, voici un exemple complet, le code est 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:=2578,
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:=2578,
recursive:=True).Execute

WB.Save
End Sub



isabelle
 
- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD
Assurez vous de marquer un message comme solution pour une meilleure transparence.

Discussions similaires

  • Question Question
Réponses
7
Affichages
376
Réponses
9
Affichages
739
Retour