Les Snippets

Connexion

Protéger et Deprotéger une feuille excel

Niveau requis pour utiliser/comprendre cette source : 1 ( Débutant )
Créé le 21/11/2006 09:22:30 et initié par Drikce06 [Liste]
Date de mise à jour : 24/11/2006 04:00:35
Vue : 12894
Catégorie(s) : Divers
Langages dispo pour ce code :
- VB 2005, VB.NET 1.x
- VB6, VBA
- VBA



Langage : VB.NET 1.x , VB 2005
Date ajout : 21/11/2006
Posté par Drikce06 [Liste]
DateMAJ : 21/11/2006

' ** Référence : Microsoft Excel 11.0 Library Objects ou équivalent
Sub SheetProtect(ByVal sBookAProteger As String, ByVal sFeuilleAProteger As String, ByVal vPassword As Object) 
If My.Computer.FileSystem.FileExists(sBookAProteger) Then
Dim xlApp As Microsoft.Office.Interop.Excel.Application 
Dim xlBook As Microsoft.Office.Interop.Excel.Workbook 
Dim i As Integer

xlApp = CreateObject("Excel.Application") 
xlBook = xlApp.Workbooks.Open(sBookAProteger)
For i = 1 To xlBook.Sheets.Count 
If xlBook.Sheets(i).Name = sFeuilleAProteger Then

xlBook.Activate()

xlBook.Sheets(sFeuilleAProteger).Select()

xlBook.Worksheets(sFeuilleAProteger).Protect(vPassword)

Exit For

ElseIf i = xlBook.Sheets.Count Then
MsgBox("La feuille à protéger n'existe pas!", vbCritical) 
End If
Next ixlBook.Close(True) 
xlApp.Quit()

xlBook = Nothing

xlApp = Nothing

Else
MsgBox("Le fichier n'existe pas, vérifier le chemin !", vbCritical) 
End If

End Sub
Sub SheetUnprotect(ByVal sBookADeproteger As String, ByVal sFeuilleADeproteger As String, ByVal vPassword As Object) 
If My.Computer.FileSystem.FileExists(sBookADeproteger) Then
Dim xlApp As Microsoft.Office.Interop.Excel.Application 
Dim xlBook As Microsoft.Office.Interop.Excel.Workbook 
Dim i As Integer

xlApp = CreateObject("Excel.Application") 
xlBook = xlApp.Workbooks.Open(sBookADeproteger)
For i = 1 To xlBook.Sheets.Count 
If xlBook.Sheets(i).Name = sFeuilleADeproteger Then

xlBook.Activate()

xlBook.Sheets(sFeuilleADeproteger).Select()

xlBook.Worksheets(sFeuilleADeproteger).Unprotect(vPassword)

Exit For

ElseIf i = xlBook.Sheets.Count Then
MsgBox("La feuille à déprotéger n'existe pas!", vbCritical) 
End If
Next ixlBook.Close(True) 
xlApp.Quit()

xlBook = Nothing

xlApp = Nothing

Else
MsgBox("Le fichier n'existe pas, vérifier le chemin !", vbCritical) 
End If

End Sub

'Exemple d'utilisation
Private Sub Button1__Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
'Protéger
Call SheetProtect("C:\Classeur1.xls", "Feuil2", 1206) 
'Déprotéger
Call SheetUnprotect("C:\Classeur1.xls", "Feuil2", 1206) 
End Sub

Langage : VB6 , VBA
Date ajout : 21/11/2006
Posté par Drikce06 [Liste]
Sub SheetProtect(ByVal sBookAProteger As String, ByVal sFeuilleAProteger As String, ByVal vPassword As Object) 
If Dir(sBookAProteger) <> "" Then
Dim xlApp As Excel.Application 
Dim xlBook As Workbook
Dim i As Integer

xlApp = CreateObject("Excel.Application") 
xlBook = xlApp.Workbooks.Open(sBookAProteger)
For i = 1 To xlBook.Sheets.Count 
If xlBook.Sheets(i).Name = sFeuilleAProteger Then

xlBook.Activate()

xlBook.Sheets(sFeuilleAProteger).Select()

xlBook.Worksheets(sFeuilleAProteger).Protect(vPassword)

Exit For

ElseIf i = xlBook.Sheets.Count Then
MsgBox("La feuille à protéger n'existe pas!", vbCritical) 
End If
Next ixlBook.Close(True) 
xlApp.Quit()

xlBook = Nothing

xlApp = Nothing

Else
MsgBox("Le fichier n'existe pas, vérifier le chemin !", vbCritical) 
End If

End Sub
Sub SheetUnprotect(ByVal sBookADeproteger As String, ByVal sFeuilleADeproteger As String, ByVal vPassword As Object) 
If Dir(sBookADeproteger) <> "" Then
Dim xlApp As Excel.Application 
Dim xlBook As Workbook
Dim i As Integer

xlApp = CreateObject("Excel.Application") 
xlBook = xlApp.Workbooks.Open(sBookADeproteger)
For i = 1 To xlBook.Sheets.Count 
If xlBook.Sheets(i).Name = sFeuilleADeproteger Then

xlBook.Activate()

xlBook.Sheets(sFeuilleADeproteger).Select()

xlBook.Worksheets(sFeuilleADeproteger).Unprotect(vPassword)

Exit For

ElseIf i = xlBook.Sheets.Count Then
MsgBox("La feuille à déprotéger n'existe pas!", vbCritical) 
End If
Next ixlBook.Close(True) 
xlApp.Quit()

xlBook = Nothing

xlApp = Nothing

Else
MsgBox("Le fichier n'existe pas, vérifier le chemin !", vbCritical) 
End If

End Sub
'Exemple d'utilisation
Private Sub Button1__Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
'Protéger
Call SheetProtect("C:\Classeur1.xls", "Feuil2", 1206) 
'Déprotéger
Call SheetUnprotect("C:\Classeur1.xls", "Feuil2", 1206) 
End Sub


Langage : VBA
Date ajout : 23/11/2006
Posté par mortalino [Liste]
DateMAJ : 24/11/2006
Function SheetsProtection(bProtect As Boolean, ParamArray SheetsNames() As Variant) As Boolean
    SheetsProtection = False
    
    Dim i              As Integer
    Dim ActualSheet    As String
        
    ActualSheet = ActiveSheet.Name
    On Local Error Resume Next
    
    For i = LBound(SheetsNames) To UBound(SheetsNames)
        Sheets(SheetsNames(i)).Select
        If Err.Number = Then Err.Clear: Exit  Function
        If bProtect Then ActiveSheet.Protect Else ActiveSheet.Unprotect
    Next i
    Sheets(ActualSheet).Select
    SheetsProtection = True
End Function

Sub Exemple_Utilisation()
' *** Retourne  True ou False, suivant le déroulement de la fonction
'     vous pouvez mettre autant de feuilles que vous le  souhaitez
    MsgBox SheetsProtection(True"Feuil1""Feuil3")
    MsgBox SheetsProtection(False"Feuil1""Feuil3")
End Sub
  

Snippets en rapport avec : Excel, Feuille, Proteger, Deproteger, Protection



Codes sources en rapport avec : Excel, Feuille, Proteger, Deproteger, Protection

{Visual Basic, VB6, VB.NET, VB 2005} CODE PERMETTANT DE MANIPULER LES OBJETS EXCEL EN .NET
Le code montre juste les grandes lignes de la manipulation d'une application Excel. ...

{Foxpro} XL8TABLE() : IMPORTE UNE FEUILLE D'UN CLASSEUR EXCEL 97 DANS UNE TABLE VISUAL FOXPRO
* Les noms des champs sont lus dans la première ligne de la feuille * Les colonnes sont importées e...

{Visual Basic, VB6, VB.NET, VB 2005} PROTECTION ET SON KEYGEN (BIS)
Arecibo avait posté la version originale ici : http://www.vbfrance.com/article.aspx?Val=9598 Il a d...

{Visual Basic, VB6, VB.NET, VB 2005} OTER LA PROTECTION D'UNE FEUILLE EXCEL PAR MACRO (TABULATIONS REFAITES)
La macro enleve la protection de la feuille excel active.Choisissez Outils, Macro, Visual Basic Edit...

{Visual Basic, VB6, VB.NET, VB 2005} MACRO POUR TRIER LES FEUILLES D'UN CLASSEUR EXCEL
Cette macro permet de trier dans l'ordre croissant les feuilles d'un classeur excel Il faut créer...

{Visual Basic, VB6, VB.NET, VB 2005} TRIER LES FEUILLES D'UN CLASSEUR EXCEL
Ce code permet de trier dans l'ordre croissant les feuilles d'un classeur excel Sur le formulaire i...

{IRC} THE AOMEUS PROJECT :P
Cet addon n'est pas totalement terminé (bugs possibles) - surtout le fichier aomeus_server.mrc - ...

{Visual Basic, VB6, VB.NET, VB 2005} MORPION2.XLS
Un petit jeu de morpion, avec lequel vous pouvez jouer contre l'ordinateur. Simple d'utilisation, re...

{IRC} ANTIBAN PROTECTION BY DJUL.
Il s'agit d'une simple protection anti ban offrant la possibilité de choisir entre différentes optio...

{PHP} TESTEUR DE MOTS DE PASSE
il s'agit d'une application permettant de vérifier le complexité du mot de passe saisi par un visite...