Compartilhar via


ChartSheet.Protect(Object, Object, Object, Object, Object) Method

Definition

Protects a ChartSheet so that it cannot be modified.

public void Protect (object Password, object DrawingObjects, object Contents, object Scenarios, object UserInterfaceOnly);
abstract member Protect : 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)

Parameters

Password
Object

A string that specifies a case-sensitive password for the worksheet or workbook. If this argument is omitted, you can unprotect the worksheet or workbook without using a password. Otherwise, you must specify the password to unprotect the worksheet or workbook. If you forget the password, you cannot unprotect the worksheet or workbook. 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. For a chart, this protects the entire chart. For a worksheet, this protects the locked cells. The default value is true.

Scenarios
Object

true to protect scenarios. This argument is valid only for worksheets. 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.

Examples

The following code example uses the Protect method to protect all contents of the current Microsoft.Office.Tools.Excel.ChartSheet. The example then uses the ProtectContents property to verify that the chart sheet is protected, and then prompts the user to remove protection from the chart sheet by using the Unprotect method.

private void ChartSheetProtection()
{
    Globals.Sheet1.Range["A1", "A5"].Value2 = 22;
    Globals.Sheet1.Range["B1", "B5"].Value2 = 55;

    this.SetSourceData(Globals.Sheet1.Range["A1", "B5"],
        Excel.XlRowCol.xlColumns);
    this.ChartType = Excel.XlChartType.xl3DColumn;

    this.Protect(true, true, true, false);

    if (this.ProtectContents)
    {
        if (DialogResult.Yes ==
            MessageBox.Show("The chart sheet is protected. " +
            "Unprotect the chart sheet?", "Example", 
            MessageBoxButtons.YesNo))
        {
            this.Unprotect();
        }
    }
}
Private Sub ChartSheetProtection()
    Globals.Sheet1.Range("A1", "A5").Value2 = 22
    Globals.Sheet1.Range("B1", "B5").Value2 = 55

    Me.SetSourceData(Globals.Sheet1.Range("A1", "B5"), _
        Excel.XlRowCol.xlColumns)
    Me.ChartType = Excel.XlChartType.xl3DColumn

    Me.Protect(DrawingObjects:=True, Contents:=True, _
        Scenarios:=True, UserInterfaceOnly:=False)

    If Me.ProtectContents Then
        If DialogResult.Yes = MessageBox.Show("The chart sheet" & _
            " is protected. Unprotect the chart sheet?", _
            "Example", MessageBoxButtons.YesNo) Then
            Me.Unprotect()
        End If
    End If
End Sub

Remarks

Use strong passwords that combine upper- and lowercase letters, numbers, and symbols. Weak passwords do not mix these elements. Strong password: Y6dh!et5. Weak password: House27. 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