password

gege21

XLDnaute Occasionnel
bonjours

peut t’ont mettre un mot de passe par macro sur VBAproject ??
si oui comment
 

Dull

XLDnaute Barbatruc
Re : password

Salut gege21, le Forum

Oui on peux

Alt + F11
Onglet Outils/Propriétés de VBAProject...
Onglet Protection
Cocher Verrouiller le projet pour l'affichage
Inscrit ton Mot de Passe et confirmes le

Click sur Ok
Enregistre et Ferme ton Fichier.

à la prochaine ouverture le Projet sera accessible seulement avec le mot de passe (Surtout ne pas l'oublier)

Bonne Journée
 

Victor21

XLDnaute Barbatruc
Re : password

Re, Bonjour, tototiti :)

Toujours sur le même site, ce code de Bill Manville :
Attention : modifier le nom du fichier à protéger !
VB:
Attribute VB_Name = "ProtegerDeprotegerVBAProject"

'Bill Manville, mpep

'================
Sub TestProtect()
  ProtectVBProject Workbooks("Perso.xls"), "zaza"
End Sub

Sub TestUnprotect()
  UnprotectVBProject Workbooks("Perso.xls"), "zaza"
  'pour qu'Excel reconnaisse la nouvelle situation (déprotection)
  DoEvents
  With Workbooks("Perso.xls")
    'ajout d'un module standard
    .VBProject.VBComponents.Add 1
  End With
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
'================
 

Pièces jointes

  • pwvba.xls
    41 KB · Affichages: 47

Victor21

XLDnaute Barbatruc
Re : password

Re, tototiti

Je ne suis pas un vbaiste confirmé, et n'ai pas décortiqué le code, mais le fichier joint montre qu'il fonctionne. (Il est nécessaire, bien entendu, de fermer le fichier pour que la protection soit effective).Seul ennui : la création à chaque fois d'un module supplémentaire -vide- dans le projet du fichier.
Mais je pense qu'il ne doit pas être difficile de supprimer un module vide. J'y retourne.
 

Victor21

XLDnaute Barbatruc
Re : password

Re

Suite :
Un code de silkyroad :
VB:
Sub supprimerTousModulesVides()
    Dim vbComp As VBComponent
    Dim i As Integer, j As Integer
    
    For Each vbComp In ActiveWorkbook.VBProject.VBComponents
        If vbComp.Type = 1 Then
            i = vbComp.CodeModule.CountOfDeclarationLines + 1
            j = vbComp.CodeModule.CountOfLines
            If j < i Then ActiveWorkbook.VBProject.VBComponents.Remove vbComp
        End If
    Next
End Sub
 

Victor21

XLDnaute Barbatruc
Re : password

Re, gege21

(Eh oui, l'habitude ici est de saluer au début de chaque post !)

Désolé, je pensais que c'était clair : pour changer le mot de passe par macro, il suffit de remplacer le mot de passe actuel "zaza" par ce que vous voulez dans la macro communiquée au #6.
 

Statistiques des forums

Discussions
312 345
Messages
2 087 470
Membres
103 551
dernier inscrit
d3vi