Back to Devexpress

DocumentSettings.WriteProtection Property

officefileapi-devexpress-dot-spreadsheet-dot-documentsettings-c420fb39.md

latest5.7 KB
Original Source

DocumentSettings.WriteProtection Property

Returns write-protection options for a workbook.

Namespace : DevExpress.Spreadsheet

Assembly : DevExpress.Spreadsheet.v25.2.Core.dll

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
WriteProtectionOptions WriteProtection { get; }
vb
ReadOnly Property WriteProtection As WriteProtectionOptions

Property Value

TypeDescription
WriteProtectionOptions

An object that stores the document’s write-protection settings.

|

Property Paths

You can access this nested property as listed below:

Object TypePath to WriteProtection
IWorkbook

.DocumentSettings .WriteProtection

| | Workbook |

.DocumentSettings .WriteProtection

|

Remarks

Write-protection options are ignored when you open a document in the WinForms or WPF Spreadsheet control. You can handle the SpreadsheetControl.DocumentLoaded event to check whether the document is write-protected (or its ReadOnlyRecommended option is enabled). If true , activate the control’s ReadOnly property.

Make a Workbook Read-Only

Use the WriteProtectionOptions.ReadOnlyRecommended option to make a workbook read-only.

csharp
using(var workbook = new Workbook()) {
    var wpOptions = workbook.DocumentSettings.WriteProtection;
    wpOptions.ReadOnlyRecommended = true;
    workbook.SaveDocument("WriteProtectedDocument.xlsx");
}
vb
Using workbook As New Workbook()
    Dim wpOptions As WriteProtectionOptions = workbook.DocumentSettings.WriteProtection
  wpOptions.ReadOnlyRecommended = True
    workbook.SaveDocument("WriteProtectedDocument.xlsx")
End Using

When users open this workbook in Microsoft® Excel®, it prompts them to open the document as read-only.

Specify a Password To Modify a Workbook

Call the WriteProtectionOptions.SetPassword method to specify a password to prevent modifications from unauthorized users.

csharp
using(var workbook = new Workbook()) {
    var wpOptions = workbook.DocumentSettings.WriteProtection;
    wpOptions.SetPassword("Password");
    wpOptions.UserName = "John Smith";
    workbook.SaveDocument("WriteProtectedDocument.xlsx");
}
vb
Using workbook As New Workbook()
    Dim wpOptions As WriteProtectionOptions = workbook.DocumentSettings.WriteProtection
    wpOptions.SetPassword("Password")
    wpOptions.UserName = "John Smith"
    workbook.SaveDocument("WriteProtectedDocument.xlsx")
End Using

When users open this workbook in Microsoft® Excel®, it prompts them to enter a password to modify the document.

Clear a Password Used to Modify a Workbook

The following code snippet uses the WriteProtectionOptions.CheckPassword method to check a given password. If the password is valid, WriteProtectionOptions.ClearPassword is called to remove write-protection from a workbook.

csharp
using (Workbook workbook = new Workbook()) {
    workbook.LoadDocument("WriteProtectedDocument.xlsx");
    RemoveWriteProtection(workbook, "password");
}
// ...

private void RemoveWriteProtection(Workbook workbook, string password) {
    var wpOptions = workbook.DocumentSettings.WriteProtection;
    if (wpOptions.IsPasswordProtected && wpOptions.CheckPassword(password))
        wpOptions.ClearPassword();
    else {
        Console.WriteLine("The file is not write-protected or the specified password is invalid!");
        Console.ReadKey();
    }
}
vb
Using workbook As New Workbook()
    workbook.LoadDocument("WriteProtectedDocument.xlsx")
    RemoveWriteProtection(workbook, "password")
End Using
' ...

Private Sub RemoveWriteProtection(workbook As Workbook, password As String)
    Dim wpOptions As WriteProtectionOptions = workbook.DocumentSettings.WriteProtection
    If wpOptions.IsPasswordProtected AndAlso wpOptions.CheckPassword(password) Then
        wpOptions.ClearPassword()
    Else
        Console.WriteLine("The file is not write-protected or the specified password is invalid!")
        Console.ReadKey()
    End If
End Sub

See Also

DocumentSettings Interface

DocumentSettings Members

DevExpress.Spreadsheet Namespace