Protecting

ActiveDocument.Protect Type:=wdProtectionType.wdAllowOnlyFormFields 
                       NoReset:=False, _
                       Password:="password", _
                       UseIRM:=False, _
                       EnforceStyleLock:=False

Type - The protection type for the specified document.
NoReset - Whather to reset form fields to their default values. The default value is False. A True value will retain make sure the form fields keep their values if the document is protected. If Type is not wdAllowOnlyFormFields, the NoReset argument is ignored.
Password - The password required to remove protection from the document.
UseIRM - Specifies whether to use Information Rights Management (IRM) when protecting the document from changes.
EnforceStyleLock - Specifies whether formatting restrictions are enforced in a protected document.


When a document is protected, users can make only limited changes, such as adding annotations, making revisions, or completing a form. If the document is already protected when you use this method, an exception is thrown.
Avoid using hard-coded passwords in your applications. If a password is required in a procedure, request the password from the user, store it in a variable, and then use the variable in your code.


Shade Editable Ranges

Application.ActiveWindow.ViewShadeEditableRanges 


Removing all editable regions

ActiveDocument.SelectAllEditableRanges() 
objRegions = ActiveDocument.ActiveWIndow.Selection.Range
If (objRegions.Editors.Count > 0) Then
   objRegions.Editors,Item(Word.wdEditorType.wdEditorEveryone),DeleteAll()
End If


UnProtecting a Document

ActiveDocument.Unprotect (Password:="") 

If ActiveDocument.ProtectionType:=wdProtectionType.wdNoProtection Then 
   ActiveDocument.Protect (wdProtectionType.wdAllowOnlyComments, , "password"
   ActiveDocument.UnProtect Password:="password"
End If

Protecting a Template

Protecting a template is confusing if a template is password protected for opening.
When you try to create a new document based on that template you will need the password


If you attempt to open a document based on a password protected template you are prompted for the password although you can just press Enter and the document is still opened.
If you set the PassTemplate to an empty string you will be prompted for the password.

Documents.Open "C:\Temp\Name.doc" PasswordTemplate = "" 

If you set the PassTemplate to an invalid string you will NOT be prompted for the password.

Documents.Open "C:\Temp\Name.doc" PasswordTemplate = "invalid" 

BUG - Unable to save a style to a Protected Template

Create a new style and tick the "add to template" checkbox.
The style will not be saved to the template but will be saved to the Normal.dot instead
There is no prompt ??


BUG - Unable to add Password to a Template for modifications

If you add a password to a template to prevent modifications, pressing ESC when prompted will open the file anyway.



objDocument.ReadOnly = True / False 
objDocument.ReadOnlyRecommended = True / False
objDocument.UnProtect "password"
objDocument.Password = "password to open file"

Determines if the document has a password which is needed to modify the document.

objDocument.HasPassword = True / False 

objDocument.Protect(Type, NoReset, Password) 


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext