Projet protégé

A

avatar

Guest
Bonjour,

Comment supprimer une macro et en recréer une autre ds ThisWorkbook sachant que le projet est protégé par password.
Qd j'essaie avec le code ci-dessous, il m'indique que c'est impossible sur un projet protégé.

With ExcelSource.VBProject.VBComponents("ThisWorkbook").CodeModule
lintDebut = .ProcStartLine("Macrosav", vbext_pk_Proc)
lintNblignes = .ProcCountLines("Macrosav", vbext_pk_Proc)
.deleteLines(lintDebut, lintNblignes)
End With

Merci.
 
@

@+Thierry

Guest
Bonjour Avatar, le Forum

Tiens j'ai ceci en stock de la part du Grand Chef à Trois Plumes, je ne sais pas si on pourrait pas faire plus simple si Ti passe par là, moi je dois filer...


ICI POUR UN CLASSEUR SUR LUI MEME



Option Explicit
Option Private Module

Const MdP = "Zaza"

Declare Function FindWindowA Lib "User32" _
                                  (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Declare Function GetForegroundWindow Lib "User32" () As Long

Declare Function SetForegroundWindow Lib "User32" _
                                (ByVal hWnd As Long) As Long
Sub Deprotege()
 
  Dim XLhWnd As Long, VBEhWnd As Long, CurhWnd As Long
  Dim ActWbk As Workbook, Wbk As Workbook
 
  On Error Resume Next
  If ThisWorkbook.VBProject.Protection = vbext_pp_none Then Exit Sub
 
  Set ActWbk = ActiveWorkbook
  CurhWnd = GetForegroundWindow
  XLhWnd = FindWindowA(vbNullString, Application.Caption)
  Application.ScreenUpdating = False
 
    With Application.VBE
        VBEhWnd = FindWindowA(vbNullString, .MainWindow.Caption)
        If CurhWnd = XLhWnd Then SetForegroundWindow VBEhWnd
          .CommandBars.FindControl(ID:=2557).Execute
            ThisWorkbook.Activate
            ThisWorkbook.VBProject.VBComponents(1).Activate
            SendKeys "~" & MdP & "~", True
                With .ActiveCodePane
                  If .CodeModule.CountOfLines = 0 Then .Window.Close
                End With
    End With
 
  SetForegroundWindow CurhWnd
  If Not ActWbk Is ThisWorkbook Then ActWbk.Activate

End Sub

Sub Reprotege()
 
  ThisWorkbook.Save
  Workbooks.Open ThisWorkbook.FullName

End Sub

'=================================================

[Testé uniquement sous Excel 2000 !!!]

La constante MdP contient le mot de passe du projet.

Attention, la macro "Reprotege" enregistre toutes les modifications
apportées au classeur.

A manipuler avec prudence et en testant bien, c'est toujours presque
aussi cochon et je n'ai peut-être pas pensé à gérer tous les cas de
figure.

Laurent Longre



ET ICI POUR ALLER VERS UN AUTRE CLASSEUR



Bon, eh bien voilà. Comme tu peux le constater, c'est excessivement
cochon.

'=================================================

Private Declare Function FindWindowA Lib "User32" _
                        (ByVal lpClassName As String, _
                        ByVal lpWindowName As String) As Long

Private Declare Function GetForegroundWindow Lib "User32" () As Long

Private Declare Function SetForegroundWindow Lib "User32" _
                        (ByVal hWnd As Long) As Long
 
Function Déprotège(Classeur As String, MdP As String) As Boolean
 
  Dim XLhWnd As Long, VBEhWnd As Long, CurhWnd As Long
  Dim Wbk As Workbook
 
  On Error Resume Next
  Set Wbk = Workbooks(Dir$(Classeur))
  On Error GoTo Fin
    If Not Wbk Is Nothing Then
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If Wbk.FullName <> Classeur Then Exit Function
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If Not Wbk.Saved Then Wbk.Save
&nbsp; &nbsp; Else: Application.ScreenUpdating = False
&nbsp; &nbsp; End If
&nbsp;
&nbsp; CurhWnd = GetForegroundWindow
&nbsp; XLhWnd = FindWindowA(vbNullString, Application.Caption)
&nbsp;
&nbsp; With Application.VBE
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VBEhWnd = FindWindowA(vbNullString, .MainWindow.Caption)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If CurhWnd = XLhWnd Then SetForegroundWindow VBEhWnd
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .CommandBars.FindControl(ID:=2557).Execute
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ' NE PAS EFFACER, même si le classeur est déjà ouvert !!!!!!
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Workbooks.Open Classeur
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If ActiveWorkbook.VBProject.Protection = vbext_pp_locked Then
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SendKeys "~" & MdP & "~", True
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ActiveCodePane.Window.Close
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End If
&nbsp; End With
&nbsp;
&nbsp; SetForegroundWindow CurhWnd
&nbsp; Déprotège = True
&nbsp; Exit Function

Fin:
End Function

Sub Test()

&nbsp; ' Déprotection du projet VBA C:\Temp\Test.xls (mot de passe "Zaza"),
&nbsp; ' Ajout d'un module standard dans ce projet, puis rétablissement
&nbsp; ' de la protection
&nbsp;
&nbsp; Const Classeur = "C:\Temp\Test.xls"
&nbsp; &nbsp; &nbsp; &nbsp; If Not Déprotège(Classeur, "Zaza") Then
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MsgBox "Erreur"
&nbsp; &nbsp; &nbsp; &nbsp; Else
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MsgBox "Projet VBA déprotégé."
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; With Workbooks(Dir$(Classeur))
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .VBProject.VBComponents.Add vbext_ct_StdModule
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Close True
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End With
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Workbooks.Open Classeur
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MsgBox "Projet reprotégé, ajout d'un module standard."
&nbsp; &nbsp; &nbsp; &nbsp; End If

End Sub

'=================================================

Testé sous Excel 2000 SR-2.

Petit inconvénient : si le classeur à "déprotéger" (cad à ouvrir en
entrant le mot de passe) est déjà ouvert au moment ou la procédure
"Déprotège" s'exécute, il sera ré-enregistré si des modifications y ont
été faites par l'utilisateur, avant la déprotection.

Laurent Longre


Ouf !

Voilà des choses excessivement cochonnes (dixit) du Grand Laurent Longre http://longre.free.fr

Bon Appétit
@+Thierry
 

Discussions similaires

Réponses
6
Affichages
518
  • Résolu(e)
Microsoft 365 Macro import
Réponses
13
Affichages
611

Statistiques des forums

Discussions
314 204
Messages
2 107 183
Membres
109 769
dernier inscrit
patbol