Share via


WorkbookBase.HasPassword 屬性

取得值,指出活頁簿是否有保護密碼。

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

語法

'宣告
Public ReadOnly Property HasPassword As Boolean
public bool HasPassword { get; }

屬性值

型別:System.Boolean
如果活頁簿有保護密碼則為 true,否則為 false。

範例

下列程式碼範例檢查 HasPassword 屬性的值,判斷活頁簿是否已設定密碼保護。 如果活頁簿未設定密碼保護,則此範例會將 Password 屬性設為使用者輸入的密碼,然後呼叫 SetPasswordEncryptionOptions 方法,設定加密 (Encryption) 演算法、金鑰 (Key) 長度、加密提供者名稱,並啟用檔案屬性加密。 然後此範例會在工作表 Sheet1 中顯示 PasswordEncryptionProviderPasswordEncryptionAlgorithmPasswordEncryptionKeyLengthPasswordEncryptionFileProperties 屬性的值。 此範例假設名為 GetPasswordFromUserInput 的方法已經存在,此方法會取得由使用者輸入的強式密碼。

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

Private Sub WorkbookPasswordOptions()
    If Not Me.HasPassword Then
        Me.Password = GetPasswordFromUserInput()
        Me.SetPasswordEncryptionOptions( _
            "Microsoft RSA SChannel Cryptographic Provider", _
            "RC4", 128, True)
    End If

    ' Display the password properties in Sheet1.
    Globals.Sheet1.Range("A1").Value2 = _
        "Password Encryption Provider:"
    Globals.Sheet1.Range("A2").Value2 = _
        "Password Encryption Algorithm:"
    Globals.Sheet1.Range("A3").Value2 = _
        "Password Encryption Key Length:"
    Globals.Sheet1.Range("A4").Value2 = _
        "Password Encryption File Properties:"
    Globals.Sheet1.Range("A1", "A4").Columns.AutoFit()

    Globals.Sheet1.Range("B1").Value2 = _
        Me.PasswordEncryptionProvider
    Globals.Sheet1.Range("B2").Value2 = _
        Me.PasswordEncryptionAlgorithm
    Globals.Sheet1.Range("B3").Value2 = _
        Me.PasswordEncryptionKeyLength
    Globals.Sheet1.Range("B4").Value2 = _
        Me.PasswordEncryptionFileProperties
    Globals.Sheet1.Range("B1", "B4").Columns.AutoFit()
End Sub
private void WorkbookPasswordOptions()
{
    if (!this.HasPassword)
    {
        this.Password = GetPasswordFromUserInput();
        this.SetPasswordEncryptionOptions(
            "Microsoft RSA SChannel Cryptographic Provider",
            "RC4", 128, true);
    }

    // Display the password properties in Sheet1.
    Globals.Sheet1.Range["A1"].Value2 =
        "Password Encryption Provider:";
    Globals.Sheet1.Range["A2"].Value2 =
        "Password Encryption Algorithm:";
    Globals.Sheet1.Range["A3"].Value2 =
        "Password Encryption Key Length:";
    Globals.Sheet1.Range["A4"].Value2 =
        "Password Encryption File Properties:";
    Globals.Sheet1.Range["A1", "A4"].Columns.AutoFit();

    Globals.Sheet1.Range["B1"].Value2 =
        this.PasswordEncryptionProvider;
    Globals.Sheet1.Range["B2"].Value2 =
        this.PasswordEncryptionAlgorithm;
    Globals.Sheet1.Range["B3"].Value2 =
        this.PasswordEncryptionKeyLength;
    Globals.Sheet1.Range["B4"].Value2 =
        this.PasswordEncryptionFileProperties;
    Globals.Sheet1.Range["B1", "B4"].Columns.AutoFit();
}

.NET Framework 安全性

請參閱

參考

WorkbookBase 類別

Microsoft.Office.Tools.Excel 命名空間