Freigeben über


WorksheetBase.Protect Method

Definition

Protects a worksheet so that it cannot be modified.

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);
member this.Protect : obj * obj * obj * obj * obj * obj * obj * obj * obj * obj * obj * obj * obj * obj * obj * obj -> unit
Public Sub Protect (Optional password As Object, Optional drawingObjects As Object, Optional contents As Object, Optional scenarios As Object, Optional userInterfaceOnly As Object, Optional allowFormattingCells As Object, Optional allowFormattingColumns As Object, Optional allowFormattingRows As Object, Optional allowInsertingColumns As Object, Optional allowInsertingRows As Object, Optional allowInsertingHyperlinks As Object, Optional allowDeletingColumns As Object, Optional allowDeletingRows As Object, Optional allowSorting As Object, Optional allowFiltering As Object, Optional allowUsingPivotTables As Object)

Parameters

password
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
Object

true to protect shapes. The default value is false.

contents
Object

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

scenarios
Object

true to protect scenarios. The default value is true.

userInterfaceOnly
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
Object

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

allowFormattingColumns
Object

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

allowFormattingRows
Object

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

allowInsertingColumns
Object

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

allowInsertingRows
Object

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

allowInsertingHyperlinks
Object

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

allowDeletingColumns
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
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
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
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
Object

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

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 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);
        }
    }
}
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

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.

Applies to