Compartir a través de


WorksheetBase.Protection (Propiedad)

Obtiene un objeto Microsoft.Office.Interop.Excel.Protection que representa las opciones de protección de la hoja de cálculo.

Espacio de nombres:  Microsoft.Office.Tools.Excel
Ensamblado:  Microsoft.Office.Tools.Excel.v4.0.Utilities (en Microsoft.Office.Tools.Excel.v4.0.Utilities.dll)

Sintaxis

'Declaración
Public ReadOnly Property Protection As Protection
public Protection Protection { get; }

Valor de propiedad

Tipo: Microsoft.Office.Interop.Excel.Protection
Objeto Microsoft.Office.Interop.Excel.Protection que representa las opciones de protección de la hoja de cálculo.

Ejemplos

El ejemplo de código siguiente obtiene el valor de la propiedad Protection para determinar si se permite el formato de celdas en el momento.Si no se permite el formato de celdas, se llama al método Protect con el parámetro AllowFormattingCells establecido en true para poder dar formato a las celdas.

Se trata de un ejemplo para una personalización en el nivel del documento.

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);
        }
    }
}

Seguridad de .NET Framework

Vea también

Referencia

WorksheetBase Clase

Microsoft.Office.Tools.Excel (Espacio de nombres)