Partilhar via


Worksheet.Protect Method

Protects a worksheet so that it cannot be modified.

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

Syntax

'Declaration
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 _
)
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.

.NET Framework Security

See Also

Reference

Worksheet Interface

Microsoft.Office.Tools.Excel Namespace