保护工作簿使其无法修改。
命名空间:  Microsoft.Office.Tools.Excel
程序集:  Microsoft.Office.Tools.Excel.v4.0.Utilities(在 Microsoft.Office.Tools.Excel.v4.0.Utilities.dll 中)
语法
声明
Public Sub Protect ( _
    password As Object, _
    structure As Object, _
    windows As Object _
)
public void Protect(
    Object password,
    Object structure,
    Object windows
)
参数
- password
 类型:System.Object
 工作簿的密码,区分大小写。如果省略此参数,则无需使用密码即可取消保护工作簿。否则,必须指定密码才能取消保护工作簿。
- structure
 类型:System.Object
 如果为 true,则保护工作簿的结构(工作表的相对位置)。默认值为 false。
- windows
 类型:System.Object
 如果为 true,则保护工作簿窗口。如果省略此参数,则窗口不受保护。
备注
使用由大写字母和小写字母、数字和符号组合而成的强密码。 弱密码不混合使用这些元素。 例如,“Y6dh!et5”是强密码,“House27”是弱密码。 使用一个方便记忆的强密码,这样就不必将它记在纸上。
可选参数
有关可选参数的信息,请参见Office 解决方案中的可选参数。
示例
下面的代码示例使用 Protect 方法在不指定密码保护的情况下保护工作簿结构和工作簿窗口。 然后,此示例检查 ProtectStructure 和 ProtectWindows 属性的值,验证已设置了此保护。
此示例针对的是文档级自定义项。
Private Sub ProtectWorkbook()
    Me.Protect(Structure:=True, Windows:=True)
    If Me.ProtectStructure Then
        MsgBox("You cannot add, delete or change the location " & _
            "of sheets in this workbook.")
    End If
    If Me.ProtectWindows Then
        MsgBox("You cannot arrange windows in this workbook.")
    End If
End Sub
private void ProtectWorkbook()
{
    this.Protect(missing, true, true);
    if (this.ProtectStructure)
    {
        MessageBox.Show("You cannot add, delete or change the location " +
            "of sheets in this workbook.");
    }
    if (this.ProtectWindows)
    {
        MessageBox.Show("You cannot arrange windows in this workbook.");
    }
}
.NET Framework 安全性
- 对直接调用方的完全信任。此成员不能由部分信任的代码使用。有关更多信息,请参见通过部分受信任的代码使用库。