PDA

View Full Version : [SOLVED:] Macro not running on a protected document



cjmitton
01-09-2014, 03:58 AM
I'm running Word 2010 (32bit) on Windows 7 Pro Desktops.I have a set of print macro's that run from a custom ribbon, they all run fine on the majority of documents but when we try to use them on our forms that are protected documents (to ensure people don't mess them up!) the Macro's fail with error 4605: "This Method or property is not available because the document is a protected document."When I debug the code, the error happens when the code adjusts the trays where the print will come from in the ActiveDocument.pagesetup.firstpagetray = 'tray number'I'm guessing this is due to the document being protected / read only so I can not adjust this. I've had a bit of a google to try and find a way of detecting if the document is protected, if it is un-protecting it running the relevant print procedure, then re-protecting the document. The forms in question do not have passwords on them so I don't need to worry about that, but I would need to put a fail safe in for documents that do have a password on. Something along the lines of trying a password of '' (blank) if it fails then to give a message box to let them know.Any thoughts please.

macropod
01-09-2014, 04:26 AM
You could test the protection state by trying to remove the forms protection, if it exists. For example:

Dim Pwd As String, pState As Variant
With ActiveDocument
pState = False
If .ProtectionType <> wdNoProtection Then
pState = .ProtectionType
Pwd = InputBox("Please enter the Password", "Password")
On Error GoTo ErrExit
.Unprotect Pwd
End If
'Do your processing here
If pState <> wdNoProtection Then .Protect Type:=pState, NoReset:=True, Password:=Pwd
.Close SaveChanges:=False '(or True, depending on what you're doing)
ErrExit:
If Err.Number = 5485 Then
MsgBox "Error: " & Err.Number & vbCr & "Unable to access:" & vbCr & .FullName
.Close SaveChanges:=False
End If
End With
Note that the above code prompts you for a password. You can omit that aspect if you wish, by deleting 'Pwd = InputBox("Please enter the Password", "Password")'

cjmitton
01-20-2014, 02:39 AM
Sorry for the delay in replying. Been busy on another project and had chance to take a look this weekend.This worked really well, thanks very much