Back to Devexpress

Workbook.Protect(String, Boolean, Boolean) Method

officefileapi-devexpress-dot-spreadsheet-dot-workbook-dot-protect-x28-system-dot-string-system-dot-boolean-system-dot-boolean-x29.md

latest5.2 KB
Original Source

Workbook.Protect(String, Boolean, Boolean) Method

Protects the structure and windows of a workbook.

You need a license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use this method in production code.

Namespace : DevExpress.Spreadsheet

Assembly : DevExpress.Docs.v25.2.dll

NuGet Package : DevExpress.Document.Processor

Declaration

csharp
public void Protect(
    string password,
    bool lockStructure,
    bool lockWindows
)
vb
Public Sub Protect(
    password As String,
    lockStructure As Boolean,
    lockWindows As Boolean
)

Parameters

NameTypeDescription
passwordString

A string that specifies a password for the workbook. If an empty string is specified, the workbook can be unprotected without a password.

| | lockStructure | Boolean |

true , to lock the structure of the workbook (the position of the sheets); otherwise, false.

| | lockWindows | Boolean |

true , to prevent users from changing the position of worksheet windows; otherwise, false.

|

Remarks

Locked structure of the workbook prevents users from adding or deleting worksheets, from hiding worksheets or displaying hidden worksheets. Locked windows option prevents users from freezing or unfreezing panes.

If the workbook is already protected (the Workbook.IsProtected property is true ), the Protect method raises an exception.

View Example

csharp
Worksheet worksheet = workbook.Worksheets["ProtectionSample"];
workbook.Worksheets.ActiveWorksheet = worksheet;

// Protect workbook structure with a password (prevent users from adding or
// deleting worksheets or displaying hidden worksheets).
if (!workbook.IsProtected)
    workbook.Protect("password", true, false);
// Add a note.
worksheet["B2"].Value = "Workbook structure is protected with a password. \n You cannot add, move or delete worksheets until protection is removed.";
worksheet.Visible = true;
vb
Dim worksheet As Worksheet = workbook.Worksheets("ProtectionSample")
workbook.Worksheets.ActiveWorksheet = worksheet

' Protect workbook structure with the password (prevent users from adding or
'deleting worksheets or displaying hidden worksheets).
If Not workbook.IsProtected Then
    workbook.Protect("password", True, False)
End If
' Add a note.
worksheet("B2").Value = "Workbook structure is protected with a password. " & ControlChars.Lf & " You cannot add, move or delete worksheets until protection is removed."
worksheet.Visible = True

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the Protect(String, Boolean, Boolean) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

spreadsheet-document-api-examples-part2/CS/SpreadsheetDocServerAPIPart2/CodeExamples/ProtectionActions.cs#L24

csharp
if (!workbook.IsProtected)
    workbook.Protect("password", true, false);
// Add a note.

spreadsheet-document-api-examples-part2/VB/SpreadsheetDocServerAPIPart2/CodeExamples/ProtectionActions.vb#L21

vb
If Not workbook.IsProtected Then
    workbook.Protect("password", True, False)
End If

Implements

Protect(String, Boolean, Boolean)

See Also

Workbook Class

Workbook Members

DevExpress.Spreadsheet Namespace