Partager via


WorkbookBase.SetPasswordEncryptionOptions Method

Sets the options for encrypting the workbook using a password.

Namespace:  Microsoft.Office.Tools.Excel
Assembly:  Microsoft.Office.Tools.Excel.v4.0.Utilities (in Microsoft.Office.Tools.Excel.v4.0.Utilities.dll)

Syntax

'Declaration
Public Sub SetPasswordEncryptionOptions ( _
    passwordEncryptionProvider As Object, _
    passwordEncryptionAlgorithm As Object, _
    passwordEncryptionKeyLength As Object, _
    passwordEncryptionFileProperties As Object _
)
public void SetPasswordEncryptionOptions(
    Object passwordEncryptionProvider,
    Object passwordEncryptionAlgorithm,
    Object passwordEncryptionKeyLength,
    Object passwordEncryptionFileProperties
)

Parameters

  • passwordEncryptionProvider
    Type: System.Object

    A case sensitive string of the encryption provider.

  • passwordEncryptionAlgorithm
    Type: System.Object

    A case sensitive string of the algorithmic short name (i.e. "RC4").

  • passwordEncryptionKeyLength
    Type: System.Object

    The encryption key length, which is a multiple of 8 (40 or greater).

  • passwordEncryptionFileProperties
    Type: System.Object

    true (default) to encrypt file properties.

Remarks

The PasswordEncryptionProvider, PasswordEncryptionAlgorithm, and PasswordEncryptionKeyLength parameters are not independent of each other. A selected encryption provider limits the set of algorithms and key length that can be chosen.

For the PasswordEncryptionKeyLength parameter there is no inherent limit on the range of the key length. The range is determined by the Cryptographic Service Provider which also determines the cryptographic algorithm.

Optional Parameters

For information on optional parameters, see Optional Parameters in Office Solutions.

Examples

The following code example checks the value of the HasPassword property to determine whether the workbook is password protected. If the workbook is not password protected, then the example sets the Password property to a password obtained from user input, and then calls the SetPasswordEncryptionOptions method to set the encryption algorithm, set the key length, set the name of the encryption provider, and enable file property encryption. The example then displays the values of the PasswordEncryptionProvider, PasswordEncryptionAlgorithm, PasswordEncryptionKeyLength, and PasswordEncryptionFileProperties properties in worksheet Sheet1. This example assumes that the method GetPasswordFromUserInput is defined elsewhere.

This example is for a document-level customization.

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 Security

See Also

Reference

WorkbookBase Class

Microsoft.Office.Tools.Excel Namespace