Introduction
To prevent anyone from accidentally or deliberately changing, moving, or deleting worksheets, you can protect workbook elements with or without a password. To protect the Structure of a workbook so that worksheets in the workbook can't be moved, deleted, hidden, unhidden, or renamed, and new worksheets can't be inserted, we should specify the ProtectionType as Structure. To protect Windows so that they are the same size and position each time the workbook is opened, we should specify the ProtectionType as Windows. We carry out these tasks (Protecting and Unprotecting workbooks) using VSTO and Aspose.Cells for .NET in contrast.
Note: Protecting a workbook does not stop users from editing cells. To protect the data, you must protect the worksheets.
Task Category: MS Excel.
Protect a Workbook
Task Description:
Open an existing excel file, Protect the workbook with Structure and Windows attributes and save the file.
Source Codes:
Following are the parallel code snippets for VSTO (C#, VB) and Aspose.Cells for .NET (C#, VB) that define how to protect a workbook.
1) VSTO
[C#]
…….
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using System.Reflection;
…….
//Instantiate the Application object.
Excel.Application excelApp = new Excel.ApplicationClass();
//Specify the template excel file path.
string myPath = @"d:\test\MyBook.xls";
//Open the excel file.
excelApp.Workbooks.Open(myPath, Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value);
//Protect the workbook specifying a password with Structure and Windows attributes.
excelApp.ActiveWorkbook.Protect("007", true, true);
//Save the file.
excelApp.ActiveWorkbook.Save();
//Quit the Application.
excelApp.Quit();
[VB]
…….
Imports Microsoft.VisualStudio.Tools.Applications.Runtime
Imports Excel = Microsoft.Office.Interop.Excel
Imports Office = Microsoft.Office.Core
Imports System.Reflection
…….
'Instantiate the Application object.
Dim excelApp As Excel.Application = New Excel.ApplicationClass()
'Specify the template excel file path.
Dim myPath As String = "d:\test\MyBook.xls"
'Open the excel file.
excelApp.Workbooks.Open(myPath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value)
'Protect the workbook specifying a password with Structure and Windows attributes.
excelApp.ActiveWorkbook.Protect("007", True, True)
'Save As the excel file.
excelApp.ActiveWorkbook.Save()
'Quit the Application.
excelApp.Quit()
2) Aspose.Cells for NET
[C#]
…….
using Aspose.Cells;
…….
//Instantiate a new Workbook.
Workbook workbook = new Workbook();
//Specify the template excel file path.
string myPath = @"d:\test\MyBook.xls";
//Open the excel file.
workbook.Open(myPath);
//Protect the workbook specifying a password with Structure and Windows attributes.
workbook.Protect(ProtectionType.All,"007");
//Save As the excel file.
workbook.Save(@"d:\test\MyBook.xls");
[VB]
…….
Imports Aspose.Cells
…….
'Instantiate a new Workbook.
Dim workbook As Workbook = New Workbook
'Specify the template excel file path.
Dim myPath As String = "d:\test\MyBook.xls"
'Open the excel file.
workbook.Open(myPath)
'Protect the workbook specifying a password with Structure and Windows attributes.
workbook.Protect(ProtectionType.All,"007")
'Save As the excel file.
workbook.Save("d:\test\MyBook.xls")
Unprotect a Workbook
To unprotect a workbook, use the following lines of code for VSTO (C#, VB) and Aspose.Cells for .NET (C#, VB).
1) VSTO
[C#]
//Unprotect the workbook specifying its password.
excelApp.ActiveWorkbook.Unprotect("007");
[VB]
'Unprotect the workbook specifying its password.
excelApp.ActiveWorkbook.Unprotect("007")
2) Aspose.Cells for .NET
[C#]
//Unprotect the workbook specifying its password.
workbook.Unprotect("007");
[VB]
'Unprotect the workbook specifying its password.
workbook.Unprotect("007")