Password related procedures in class Document (Word VBA)

This page presents procedures from class Document related to the theme Password: HasPassword, Password, PasswordEncryptionAlgorithm, PasswordEncryptionFileProperties, PasswordEncryptionKeyLength, PasswordEncryptionProvider, ProtectionType, SetPasswordEncryptionOptions and WritePassword

HasPassword

True if a password is required to open the specified document.

ActiveDocument.Password = "kittycat" 
If ActiveDocument.HasPassword = True Then _ 
 MsgBox "The password is set."

Password

Sets a password that must be supplied to open the specified document. Write-only String.

Set myDoc = Documents _ 
 .Open(FileName:="C:\My Documents\Earnings.doc") 
myDoc.Password = strPassword 
myDoc.Close

PasswordEncryptionAlgorithm

Returns a String indicating the algorithm Microsoft Word uses for encrypting documents with passwords.

Use the SetPasswordEncryptionOptions method to specify the algorithm Word uses for encrypting documents with passwords.

Sub PasswordSettings() 
 With ActiveDocument 
 If .PasswordEncryptionAlgorithm = "OfficeXor" Then 
 .SetPasswordEncryptionOptions _ 
 PasswordEncryptionProvider:="Microsoft RSA SChannel Cryptographic Provider", _ 
 PasswordEncryptionAlgorithm:="RC4", _ 
 PasswordEncryptionKeyLength:=56, _ 
 PasswordEncryptionFileProperties:=True 
 End If 
 End With 
End Sub

PasswordEncryptionFileProperties

True if Microsoft Word encrypts file properties for password-protected documents.

Use the SetPasswordEncryptionOptions method to specify whether Word encrypts file properties for password-protected documents.

Sub PasswordSettings() 
 With ActiveDocument 
 If .PasswordEncryptionFileProperties = False Then 
 .SetPasswordEncryptionOptions _ 
 PasswordEncryptionProvider:="Microsoft RSA SChannel Cryptographic Provider", _ 
 PasswordEncryptionAlgorithm:="RC4", _ 
 PasswordEncryptionKeyLength:=56, _ 
 PasswordEncryptionFileProperties:=True 
 End If 
 End With 
End Sub

PasswordEncryptionKeyLength

Returns a Long indicating the key length of the algorithm Microsoft Word uses when encrypting documents with passwords.

Use the SetPasswordEncryptionOptions method to specify the key length Word uses when encrypting documents with passwords.

Sub PasswordSettings() 
 With ActiveDocument 
 If .PasswordEncryptionKeyLength < 40 Then 
 .SetPasswordEncryptionOptions _ 
 PasswordEncryptionProvider:="Microsoft RSA SChannel Cryptographic Provider", _ 
 PasswordEncryptionAlgorithm:="RC4", _ 
 PasswordEncryptionKeyLength:=56, _ 
 PasswordEncryptionFileProperties:=True 
 End If 
 End With 
End Sub

PasswordEncryptionProvider

Returns a String specifying the name of the algorithm encryption provider that Microsoft Word uses when encrypting documents with passwords.

Use the SetPasswordEncryptionOptions method to specify the name of the algorithm encryption provider Word uses when encrypting documents with passwords.

Sub PasswordSettings() 
 With ActiveDocument 
 If .PasswordEncryptionProvider <> "Microsoft RSA SChannel Cryptographic Provider" Then 
 .SetPasswordEncryptionOptions _ 
 PasswordEncryptionProvider:="Microsoft RSA SChannel Cryptographic Provider", _ 
 PasswordEncryptionAlgorithm:="RC4", _ 
 PasswordEncryptionKeyLength:=56, _ 
 PasswordEncryptionFileProperties:=True 
 End If 
 End With 
End Sub

ProtectionType

Returns the protection type for the specified document. Can be one of the following WdProtectionType constants: wdAllowOnlyComments, wdAllowOnlyFormFields, wdAllowOnlyReading, wdAllowOnlyRevisions, or wdNoProtection.

Possible Values are wdAllowOnlyComments - Allow only comments to be added to the document, wdAllowOnlyFormFields - Allow content to be added to the document only through form fields, wdAllowOnlyReading - Allow read-only access to the document, wdAllowOnlyRevisions - Allow only revisions to be made to existing content, wdNoProtection - Do not apply protection to the document.
If ActiveDocument.ProtectionType = wdNoProtection Then 
 ActiveDocument.Protect Type:=wdAllowOnlyComments 
End If

SetPasswordEncryptionOptions

Sets the options Microsoft Word uses for encrypting documents with passwords.

For enhanced security, do not use Weak Encryption (XOR) (also called "OfficeXor") or "Office97/2000 Compatible" (also called "OfficeStandard") algorithms.

Syntax : expression.SetPasswordEncryptionOptions (PasswordEncryptionProvider, PasswordEncryptionAlgorithm, PasswordEncryptionKeyLength, PasswordEncryptionFileProperties)

Sub PasswordSettings() 
 With ActiveDocument 
 If .PasswordEncryptionAlgorithm = "OfficeXor" Or _ 
 .PasswordEncryptionAlgorithm = "OfficeStandard" Then 
 
 .SetPasswordEncryptionOptions _ 
 PasswordEncryptionProvider:="Microsoft RSA SChannel Cryptographic Provider", _ 
 PasswordEncryptionAlgorithm:="RC4", _ 
 PasswordEncryptionKeyLength:=56, _ 
 PasswordEncryptionFileProperties:=True 
 
 End If 
 End With 
End Sub

Arguments

The following arguments are required:

PasswordEncryptionProvider (String) - The name of the encryption provider.

PasswordEncryptionAlgorithm (String) - The name of the encryption algorithm. Word supports stream-encrypted algorithms.

PasswordEncryptionKeyLength (Long) - The encryption key length. Must be a multiple of 8, starting at 40.

The following argument is optional

PasswordEncryptionFileProperties (Boolean) - True for Word to encrypt file properties. Default is True.

WritePassword

Sets a password for saving changes to the specified document. Write-only String.

Set myDoc = ActiveDocument 
If myDoc.WriteReserved = False Then myDoc.WritePassword = "secret"