This documentation is archived and is not being maintained.

Workbook.Protect Method

Protects a workbook so that it cannot be modified.

Namespace: Microsoft.Office.Tools.Excel
Assembly: Microsoft.Office.Tools.Excel (in microsoft.office.tools.excel.dll)

public virtual void Protect (
	[OptionalAttribute] Object Password,
	[OptionalAttribute] Object Structure,
	[OptionalAttribute] Object Windows
)

Parameters

Password

A case-sensitive password for the workbook. If this argument is omitted, you can unprotect the workbook without using a password. Otherwise, you must specify the password to unprotect the workbook.

Structure

true to protect the structure of the workbook (the relative position of the sheets). The default value is false.

Windows

true to protect the workbook windows. If this argument is omitted, the windows are not protected.

Use strong passwords that combine uppercase and lowercase letters, numbers, and symbols. Weak passwords do not mix these elements. For example, "Y6dh!et5" is a strong password, but "House27" is a weak password. Use a strong password that you can remember so that you do not have to write it down.

Optional Parameters

For information on optional parameters, see Understanding Optional Parameters in COM Interop.

The following code example uses the Protect method to protect the structure of the workbook and workbook windows, but without specifying password protection. The example then checks the values of the ProtectStructure and ProtectWindows properties to verify that this protection is set.

private void ProtectWorkbook()
{
    this.Protect(missing, true, true);

    if (this.ProtectStructure)
    {
        MessageBox.Show("You cannot add, delete or change the location " +
            "of sheets in this workbook.");
    }

    if (this.ProtectWindows)
    {
        MessageBox.Show("You cannot arrange windows in this workbook.");
    }
}

Show: