Share via


WorksheetBase.Protection 屬性

取得 Microsoft.Office.Interop.Excel.Protection 物件,表示工作表的保護選項。

命名空間:  Microsoft.Office.Tools.Excel
組件:  Microsoft.Office.Tools.Excel.v4.0.Utilities (在 Microsoft.Office.Tools.Excel.v4.0.Utilities.dll 中)

語法

'宣告
Public ReadOnly Property Protection As Protection
public Protection Protection { get; }

屬性值

型別:Microsoft.Office.Interop.Excel.Protection
Microsoft.Office.Interop.Excel.Protection 物件,表示工作表的保護選項。

範例

下列程式碼會取得 Protection 屬性的值,判斷是否允許儲存格格式設定。 如果不允許儲存格格式設定,就會搭配設為 true 的 AllowFormattingCells 參數呼叫 Protect 方法,讓儲存格可以進行格式化。

這是示範文件層級自訂的範例。

Private Sub ProtectCellFormatting()

    ' Protect cell contents, but do not change any 
    ' other protection type. This enables the Protection
    ' property to have effect.
    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)

    If Not Me.Protection.AllowFormattingCells Then
        If DialogResult.Yes = MessageBox.Show("Formatting cells is " & _
            "not allowed. Allow formatting cells?", "Example", _
            MessageBoxButtons.YesNo) Then

            ' Allow cell formatting, but do not change any other protection type.
            Me.Protect(DrawingObjects:=Me.ProtectDrawingObjects, _
                Contents:=Me.ProtectContents, Scenarios:=Me.ProtectScenarios, _
                UserInterfaceOnly:=Me.ProtectionMode, _
                AllowFormattingCells:=True, _
                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 ProtectCellFormatting()
{
    // Protect cell contents, but do not change any 
    // other protection type. This enables the Protection
    // property to have effect.
    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);

    if (!this.Protection.AllowFormattingCells)
    {
        if (DialogResult.Yes == MessageBox.Show("Formatting cells is " +
            "not allowed. Allow formatting cells?", "Example", 
            MessageBoxButtons.YesNo))
        {
            // Allow cell formatting, but do not change any other protection type.
            this.Protect(this.ProtectDrawingObjects, 
                this.ProtectContents, this.ProtectScenarios, 
                this.ProtectionMode, true, 
                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 安全性

請參閱

參考

WorksheetBase 類別

Microsoft.Office.Tools.Excel 命名空間