Worksheet.ProtectContents Property (2007 System)

Gets a value that indicates whether the contents of the worksheet (the individual cells) are protected.

Namespace:  Microsoft.Office.Tools.Excel
Assembly:  Microsoft.Office.Tools.Excel.v9.0 (in Microsoft.Office.Tools.Excel.v9.0.dll)

Syntax

'Declaration
Public ReadOnly Property ProtectContents As Boolean
'Usage
Dim instance As Worksheet 
Dim value As Boolean 

value = instance.ProtectContents
public bool ProtectContents { get; }
public:
property bool ProtectContents {
    bool get ();
}
public function get ProtectContents () : boolean

Property Value

Type: System.Boolean
true if the contents of the sheet are protected; otherwise, 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 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
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(missing, 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);
        }
    }
}

.NET Framework Security

See Also

Reference

Worksheet Class

Worksheet Members

Microsoft.Office.Tools.Excel Namespace