PDA

View Full Version : check if doc is protected AND for specific password (test password)



WallIT
02-15-2013, 10:21 AM
Hi,

We have a number of templates in use at our office. Some are protected for filling in forms. We use the same password across all our protected templates.

We use a custom toolbar which has a few printing macros. These macros change the page setup properties (first page, other pages) and select specific printers and print the document.

Obviously these macros don't work for protected documents, so we have worked around this by created template-specific macros for printing. These macros unprotect the document, change the page setup properties, reprotect the document and print it.

We are soon to introduce lots more templates and managing all these unique template-specific macros will become a headache! A better solution would be to enable our custom toolbar macro to be to detect if a document was protected, test it for our standard password, and if there is a match unprotect it, change the necessary settings and reprotect it.

Obviously there may be other documents (from outside) which are protected with other passwords, which I don't want the macro to attempt to unprotect.

I have found

If ActiveDocument.ProtectionType = wdNoProtection Then

which tests for protected documents, but is it possible to then test for a specific password (without showing an error if incorrect)?

Thanks in advance.

gmaxey
02-15-2013, 02:29 PM
Sometimes errors are good:

Option Explicit
Const strPW As String = "yourpassword"
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
On Error GoTo Err_PW
ActiveDocument.Unprotect strPW
MsgBox "Did something"
ActiveDocument.Protect wdAllowOnlyFormFields, True, strPW
End If
Err_PWReentry:
Exit Sub
Err_PW:
MsgBox "Sorry this isn't one of our protected documents so nothing happens."
Resume Err_PWReentry
End Sub

WallIT
02-19-2013, 08:45 AM
Greg,

This is excellent and the code works flawlessly. I really appreciate your help.

I have a few other questions related to printing. I inherited some templates, but from even my sparse knowledge of VBA can easily spot repeated and inefficient code. Please lookout for my other questions over the next few days.

Thanks again.