WorksheetBase.Protect Method

Protects a worksheet so that it cannot be modified.

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

Syntax

'Declaration
Public Sub Protect ( _
    password As Object, _
    drawingObjects As Object, _
    contents As Object, _
    scenarios As Object, _
    userInterfaceOnly As Object, _
    allowFormattingCells As Object, _
    allowFormattingColumns As Object, _
    allowFormattingRows As Object, _
    allowInsertingColumns As Object, _
    allowInsertingRows As Object, _
    allowInsertingHyperlinks As Object, _
    allowDeletingColumns As Object, _
    allowDeletingRows As Object, _
    allowSorting As Object, _
    allowFiltering As Object, _
    allowUsingPivotTables As Object _
)
public void Protect(
    Object password,
    Object drawingObjects,
    Object contents,
    Object scenarios,
    Object userInterfaceOnly,
    Object allowFormattingCells,
    Object allowFormattingColumns,
    Object allowFormattingRows,
    Object allowInsertingColumns,
    Object allowInsertingRows,
    Object allowInsertingHyperlinks,
    Object allowDeletingColumns,
    Object allowDeletingRows,
    Object allowSorting,
    Object allowFiltering,
    Object allowUsingPivotTables
)

Parameters

  • password
    Type: System.Object

    A case-sensitive password for the worksheet. If this argument is omitted, you can unprotect the worksheet without using a password. Otherwise, you must specify the password to unprotect the worksheet. If you forget the password, you cannot unprotect the worksheet. It is a good idea to keep a list of your passwords and their corresponding document names in a safe place.

  • drawingObjects
    Type: System.Object

    true to protect shapes. The default value is false.

  • contents
    Type: System.Object

    true to protect contents in the locked cells. The default value is true.

  • scenarios
    Type: System.Object

    true to protect scenarios. The default value is true.

  • userInterfaceOnly
    Type: System.Object

    true to protect the user interface, but not macros. If this argument is omitted, protection applies both to macros and to the user interface.

  • allowFormattingCells
    Type: System.Object

    true allows the user to format any cell on a protected worksheet. The default value is false.

  • allowFormattingColumns
    Type: System.Object

    true allows the user to format any column on a protected worksheet. The default value is false.

  • allowFormattingRows
    Type: System.Object

    true allows the user to format any row on a protected worksheet. The default value is false.

  • allowInsertingColumns
    Type: System.Object

    true allows the user to insert columns on the protected worksheet. The default value is false.

  • allowInsertingRows
    Type: System.Object

    true allows the user to insert rows on the protected worksheet. The default value is false.

  • allowInsertingHyperlinks
    Type: System.Object

    true allows the user to insert hyperlinks on the worksheet. The default value is false.

  • allowDeletingColumns
    Type: System.Object

    true allows the user to delete columns on the protected worksheet, where every cell in the column to be deleted is unlocked. The default value is false.

  • allowDeletingRows
    Type: System.Object

    true allows the user to delete rows on the protected worksheet, where every cell in the row to be deleted is unlocked. The default value is false.

  • allowSorting
    Type: System.Object

    true allows the user to sort on the protected worksheet. Every cell in the sort range must be unlocked or unprotected. The default value is false.

  • allowFiltering
    Type: System.Object

    true allows the user to set filters on the protected worksheet. Users can change filter criteria but can not enable or disable an autofilter. Users can set filters on an existing autofilter. The default value is false.

  • allowUsingPivotTables
    Type: System.Object

    true allows the user to use pivot table reports on the protected worksheet. The default value is false.

Remarks

If you apply this method with the UserInterfaceOnly argument set to true and then save the workbook, the entire worksheet (not just the interface) will be fully protected when you reopen the workbook. To re-enable the user interface protection after the workbook is opened, you must again apply this method with UserInterfaceOnly set to true.

It is possible to make changes to a protected worksheet if the password is supplied. Also, another method would be to unprotect the worksheet, make the necessary changes, and then protect the worksheet again.

Note

'Unprotected' means the cell may be locked (Format Cells dialog) but is included in a range defined in the Allow Users to Edit Ranges dialog, and the user has unprotected the range with a password or been validated via NT permissions.

Optional Parameters

For information on optional parameters, see Optional Parameters in Office Solutions.

Examples

The following code example gets the value of the ProtectContents property to determine whether the contents of the worksheet are protected. If the contents are unprotected, then the Protect method is called with the Contents parameter set to true so that the contents are protected.

This example is for a document-level customization.

Private Sub ProtectCellContents()
    If Not Me.ProtectContents Then 
        If DialogResult.Yes = MessageBox.Show("Cell contents in this " & _
            "worksheet are not protected. Protect cell contents?", "Example", _
            MessageBoxButtons.YesNo) Then 

            ' Protect cell contents, but do not change any  
            ' other protection type. 
            Me.Protect(DrawingObjects:=Me.ProtectDrawingObjects, _
                Contents:=True, Scenarios:=Me.ProtectScenarios, _
                UserInterfaceOnly:=Me.ProtectionMode, _
                AllowFormattingCells:=Me.Protection.AllowFormattingCells, _
                AllowFormattingColumns:=Me.Protection.AllowFormattingColumns, _
                AllowFormattingRows:=Me.Protection.AllowFormattingRows, _
                AllowInsertingColumns:=Me.Protection.AllowInsertingColumns, _
                AllowInsertingRows:=Me.Protection.AllowInsertingRows, _
                AllowInsertingHyperlinks:=Me.Protection.AllowInsertingHyperlinks, _
                AllowDeletingColumns:=Me.Protection.AllowDeletingColumns, _
                AllowDeletingRows:=Me.Protection.AllowDeletingRows, _
                AllowSorting:=Me.Protection.AllowSorting, _
                AllowFiltering:=Me.Protection.AllowFiltering, _
                AllowUsingPivotTables:=Me.Protection.AllowUsingPivotTables)
        End If 
    End If 
End Sub
private void ProtectCellContents()
{
    if (!this.ProtectContents)
    {
        if (DialogResult.Yes == MessageBox.Show("Cell contents in this " +
            "worksheet are not protected. Protect cell contents?", "Example", 
            MessageBoxButtons.YesNo))
        {
            // Protect cell contents, but do not change any  
            // other protection type. 
            this.Protect(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);
        }
    }
}

.NET Framework Security

See Also

Reference

WorksheetBase Class

Microsoft.Office.Tools.Excel Namespace