Expand Minimize

WorksheetBase.Protection Property

Gets a Microsoft.Office.Interop.Excel.Protection object that represents the protection options of the worksheet.

Namespace:  Microsoft.Office.Tools.Excel
Assembly:  Microsoft.Office.Tools.Excel.v4.0.Utilities (in Microsoft.Office.Tools.Excel.v4.0.Utilities.dll)

public Protection Protection { get; }

Property Value

Type: Microsoft.Office.Interop.Excel.Protection
A Microsoft.Office.Interop.Excel.Protection object that represents the protection options of the worksheet.

The following code example gets the value of the Protection property to determine whether cell formatting is currently allowed. If cell formatting is not allowed, then the Protect method is called with the AllowFormattingCells parameter set to true so that cells can be formatted.

This example is for a document-level customization.

private void ProtectCellFormatting()
{
    // Protect cell contents, but do not change any  
    // other protection type. This enables the Protection 
    // property to have effect. 
    this.Protect(missing, this.ProtectDrawingObjects,
        true, this.ProtectScenarios, this.ProtectionMode,
        this.Protection.AllowFormattingCells,
        this.Protection.AllowFormattingColumns,
        this.Protection.AllowFormattingRows,
        this.Protection.AllowInsertingColumns,
        this.Protection.AllowInsertingRows,
        this.Protection.AllowInsertingHyperlinks,
        this.Protection.AllowDeletingColumns,
        this.Protection.AllowDeletingRows,
        this.Protection.AllowSorting,
        this.Protection.AllowFiltering,
        this.Protection.AllowUsingPivotTables);

    if (!this.Protection.AllowFormattingCells)
    {
        if (DialogResult.Yes == MessageBox.Show("Formatting cells is " +
            "not allowed. Allow formatting cells?", "Example", 
            MessageBoxButtons.YesNo))
        {
            // Allow cell formatting, but do not change any other protection type. 
            this.Protect(this.ProtectDrawingObjects, 
                this.ProtectContents, this.ProtectScenarios, 
                this.ProtectionMode, true, 
                this.Protection.AllowFormattingColumns, 
                this.Protection.AllowFormattingRows, 
                this.Protection.AllowInsertingColumns, 
                this.Protection.AllowInsertingRows, 
                this.Protection.AllowInsertingHyperlinks, 
                this.Protection.AllowDeletingColumns,
                this.Protection.AllowDeletingRows, 
                this.Protection.AllowSorting,
                this.Protection.AllowFiltering, 
                this.Protection.AllowUsingPivotTables);
        }
    }
}

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft