Partager via


WorkbookBase.Protect(Object, Object, Object) Method

Definition

Protects a workbook so that it cannot be modified.

public void Protect (object password, object structure, object windows);
member this.Protect : obj * obj * obj -> unit
Public Sub Protect (Optional password As Object, Optional structure As Object, Optional windows As Object)

Parameters

password
Object

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
Object

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

windows
Object

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

Examples

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.

This example is for a document-level customization.

private void ProtectWorkbook()
{
    this.Protect( 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.");
    }
}
Private Sub ProtectWorkbook()
    Me.Protect(Structure:=True, Windows:=True)

    If Me.ProtectStructure Then
        MsgBox("You cannot add, delete or change the location " & _
            "of sheets in this workbook.")
    End If

    If Me.ProtectWindows Then
        MsgBox("You cannot arrange windows in this workbook.")
    End If
End Sub

Remarks

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 Optional Parameters in Office Solutions.

Applies to