次の方法で共有


Worksheet.Protect メソッド (2007 System)

更新 : 2007 年 11 月

ワークシートを変更できないよう保護します。

名前空間 :  Microsoft.Office.Tools.Excel
アセンブリ :  Microsoft.Office.Tools.Excel.v9.0 (Microsoft.Office.Tools.Excel.v9.0.dll 内)

構文

'宣言
Public Sub Protect ( _
    Password As Object, _
    DrawingObjects As Object, _
    Contents As Object, _
    Scenarios As Object, _
    UserInterfaceOnly As Object, _
    AllowFormattingCells As Object, _
    AllowFormattingColumns As Object, _
    AllowFormattingRows As Object, _
    AllowInsertingColumns As Object, _
    AllowInsertingRows As Object, _
    AllowInsertingHyperlinks As Object, _
    AllowDeletingColumns As Object, _
    AllowDeletingRows As Object, _
    AllowSorting As Object, _
    AllowFiltering As Object, _
    AllowUsingPivotTables As Object _
)
'使用
Dim instance As Worksheet
Dim Password As Object
Dim DrawingObjects As Object
Dim Contents As Object
Dim Scenarios As Object
Dim UserInterfaceOnly As Object
Dim AllowFormattingCells As Object
Dim AllowFormattingColumns As Object
Dim AllowFormattingRows As Object
Dim AllowInsertingColumns As Object
Dim AllowInsertingRows As Object
Dim AllowInsertingHyperlinks As Object
Dim AllowDeletingColumns As Object
Dim AllowDeletingRows As Object
Dim AllowSorting As Object
Dim AllowFiltering As Object
Dim AllowUsingPivotTables As Object

instance.Protect(Password, DrawingObjects, _
    Contents, Scenarios, UserInterfaceOnly, _
    AllowFormattingCells, AllowFormattingColumns, _
    AllowFormattingRows, AllowInsertingColumns, _
    AllowInsertingRows, AllowInsertingHyperlinks, _
    AllowDeletingColumns, AllowDeletingRows, _
    AllowSorting, AllowFiltering, AllowUsingPivotTables)
public void Protect(
    Object Password,
    Object DrawingObjects,
    Object Contents,
    Object Scenarios,
    Object UserInterfaceOnly,
    Object AllowFormattingCells,
    Object AllowFormattingColumns,
    Object AllowFormattingRows,
    Object AllowInsertingColumns,
    Object AllowInsertingRows,
    Object AllowInsertingHyperlinks,
    Object AllowDeletingColumns,
    Object AllowDeletingRows,
    Object AllowSorting,
    Object AllowFiltering,
    Object AllowUsingPivotTables
)

パラメータ

  • Password
    型 : System.Object

    ワークシートのパスワードです (大文字と小文字を区別します)。この引数を省略すると、パスワードを使用せずにワークシートの保護を解除できます。それ以外の場合、ワークシートの保護を解除するにはパスワードを入力する必要があります。パスワードを忘れると、ワークシートの保護を解除できなくなります。パスワードとそれに対応するドキュメント名の一覧を安全な場所に保管することをお勧めします。

  • DrawingObjects
    型 : System.Object

    図形を保護する場合は true です。既定値は、false です。

  • Contents
    型 : System.Object

    ロックされたセルの内容を保護する場合は true です。既定値は、true です。

  • Scenarios
    型 : System.Object

    シナリオを保護する場合は true です。既定値は、true です。

  • UserInterfaceOnly
    型 : System.Object

    ユーザー インターフェイスを保護し、マクロを保護しない場合は true です。この引数が省略されると、マクロとユーザー インターフェイスの両方が保護されます。

  • AllowFormattingCells
    型 : System.Object

    true に設定すると、保護されているワークシート内の任意のセルを書式設定できます。既定値は、false です。

  • AllowFormattingColumns
    型 : System.Object

    true に設定すると、保護されているワークシート内の任意の列を書式設定できます。既定値は、false です。

  • AllowFormattingRows
    型 : System.Object

    true に設定すると、保護されているワークシート内の任意の行を書式設定できます。既定値は、false です。

  • AllowInsertingColumns
    型 : System.Object

    true に設定すると、保護されているワークシートに列を挿入できます。既定値は、false です。

  • AllowInsertingRows
    型 : System.Object

    true に設定すると、保護されているワークシートに行を挿入できます。既定値は、false です。

  • AllowInsertingHyperlinks
    型 : System.Object

    true に設定すると、ワークシートにハイパーリンクを挿入できます。既定値は、false です。

  • AllowDeletingColumns
    型 : System.Object

    true に設定すると、削除する列のすべてのセルのロックを解除して、保護されているワークシートの列を削除できます。既定値は、false です。

  • AllowDeletingRows
    型 : System.Object

    true に設定すると、削除する行のすべてのセルのロックを解除して、保護されているワークシートの行を削除できます。既定値は、false です。

  • AllowSorting
    型 : System.Object

    true に設定すると、保護されているワークシートを並べ替えることができます。並べ替えの範囲内にあるすべてのセルのロックが解除されるか、保護が解除されている必要があります。既定値は、false です。

  • AllowFiltering
    型 : System.Object

    true に設定すると、保護されているワークシートにフィルタを設定できます。ユーザーは、フィルタ条件を変更することはできますが、オートフィルタを有効または無効にすることはできません。既存のオートフィルタにフィルタを設定することはできます。既定値は、false です。

  • AllowUsingPivotTables
    型 : System.Object

    true に設定すると、保護されているワークシートでピボット テーブル レポートを使用できます。既定値は、false です。

解説

UserInterfaceOnly 引数を true に設定してこのメソッドを適用してブックを保存すると、ブックを再度開いたときにワークシート全体 (インターフェイス以外も) が完全に保護されます。ブックを開いた後でユーザー インターフェイスの保護を再度有効にするには、UserInterfaceOnly を true に設定して再度このメソッドを適用する必要があります。

パスワードを入力すると、保護されたワークシートを変更できます。さらに、他のメソッドがワークシートの保護を解除しようとした場合、必要な変更を加え、ワークシートを再度保護します。

メモ :

"保護されていない" とは、セルがロックされている可能性はあるが ([セルの書式設定] ダイアログ ボックス)、[範囲の編集を可能にする] ダイアログ ボックスで定義された範囲に含まれており、ユーザーがこの範囲をパスワードによって保護を解除したか、ユーザーが NT アクセス許可によって確認されている場合を指します。

省略可能なパラメータ

省略可能なパラメータについては、「Office ソリューションの省略可能なパラメータについて」を参照してください。

次のコード例では、ProtectContents プロパティの値を取得し、ワークシートの内容が保護されているかどうかを確認します。内容が保護されていない場合、true に設定された Contents パラメータと共に Protect メソッドが呼び出され、内容が保護されます。

この例は、ドキュメント レベルのカスタマイズ用に作成されています。

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

アクセス許可

  • 直前の呼び出し元に対する完全な信頼。このメンバは、部分的に信頼されているコードから使用することはできません。詳細については、「部分信頼コードからのライブラリの使用」を参照してください。

参照

参照

Worksheet クラス

Worksheet メンバ

Microsoft.Office.Tools.Excel 名前空間