Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use ActiveDocument.Unprotect only if document is protected 2

Status
Not open for further replies.

dotobi

Technical User
Mar 9, 2004
229
HK
Is there a way to only use "ActiveDocument.Unprotect" if the docuemnt is protected in the 1st place?

And then the code should use "ActiveDocument.Protect" at the end, only if the document was protected in the 1st place.

This is in realation to a word 2003 macro (
Thanks

Kev
 
A starting point:
If ActiveDocument.ProtectionType <> wdNoProtection Then ActiveDocument.Unprotect

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks for that.

All I'm trying to do is make the macro run whether the doc is protected or not. Is this the best approach?

Thanks
 
Why didn't you reply the answer you've got in the above thread ?
Is this the best approach?
To do WHAT ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 

> Is this the best approach?

I think it's the only approach (bar an error trap) if you need the document unprotected but, if you want to restore the original protection, you will need to remember what it was ...
Code:
[blue]Dim OriginalProtection As WdProtectionType

OriginalProtection = ActiveDocument.ProtectionType
If OriginalProtection <> wdNoProtection Then ActiveDocument.Unprotect


[green]' Later[/green]
If OriginalProtection <> wdNoProtection Then ActiveDocument.Protect OriginalProtection
[/blue]
Depending on the type of protection you may want to use some other parameters on the Protect Call.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
Hi There

Thanks for that.

It works :)

>I think it's the only approach (bar an error trap) if you need the document unprotected (TonyJollans)...
All I need is for the macro to run on 'any' document protected or not. If there is a way to make the macro run without having to 1st unprotect 'protected' documents, then that would be favorable (especially if I'm ever dealing with a doc protect by a password).

However, the code I have does what I need it to do. Thank you!

Why didn't you reply the answer you've got in the above thread? (PHV)...
This was a different problem, so I felt that using a new thread with a subject highlighting the specific problem would help the right person find the post and would help other people with the same problem find it in the future. Thanks for your help with the .ActivePrinter problem!

This is the final code. If anyone has any suggestions for how it could be inproved, please let me know (I'm very new to this).

Code:
Sub printLHPL()
Dim strPrinter As String
strPrinter = Application.ActivePrinter
Application.WordBasic.FilePrintSetup Printer:="\\servername\printername on printerport", _
    DoNotSetAsSysDefault:=1
Dim OriginalProtection As WdProtectionType
OriginalProtection = ActiveDocument.ProtectionType
If OriginalProtection <> wdNoProtection Then ActiveDocument.Unprotect
    With ActiveDocument.Styles(wdStyleNormal).Font
        If .NameFarEast = .NameAscii Then
            .NameAscii = ""
        End If
        .NameFarEast = ""
    End With
    With ActiveDocument.PageSetup
        .FirstPageTray = wdPrinterLowerBin
        .OtherPagesTray = wdPrinterPaperCassette
    End With
    ActiveDocument.PrintOut
    With ActiveDocument.Styles(wdStyleNormal).Font
        If .NameFarEast = .NameAscii Then
            .NameAscii = ""
        End If
        .NameFarEast = ""
    End With
    With ActiveDocument.PageSetup
        .FirstPageTray = wdPrinterPaperCassette
        .OtherPagesTray = wdPrinterPaperCassette
    End With
    ActiveDocument.PrintOut
Application.ActivePrinter = strPrinter
If OriginalProtection <> wdNoProtection Then ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub

Thanks

keV
 
Sorry - this is actually the final code:

Code:
Sub printLHPL()
Dim strPrinter As String
strPrinter = Application.ActivePrinter
Application.WordBasic.FilePrintSetup Printer:=":="\\servername\printername on printerport", _
    DoNotSetAsSysDefault:=1
Dim OriginalProtection As WdProtectionType
OriginalProtection = ActiveDocument.ProtectionType
If OriginalProtection <> wdNoProtection Then ActiveDocument.Unprotect
    With ActiveDocument.PageSetup
        .FirstPageTray = wdPrinterLowerBin
        .OtherPagesTray = wdPrinterPaperCassette
    End With
    ActiveDocument.PrintOut
    With ActiveDocument.PageSetup
        .FirstPageTray = wdPrinterPaperCassette
        .OtherPagesTray = wdPrinterPaperCassette
    End With
    ActiveDocument.PrintOut
Application.ActivePrinter = strPrinter
If OriginalProtection <> wdNoProtection Then ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub
 
I would like to point out a wee technical issue with explicit values.

The code
Code:
Dim OriginalProtection As WdProtectionType
OriginalProtection = ActiveDocument.ProtectionType
can return OriginalProtection as FOUR possible values.

-1, 0, 1, 2

the code
Code:
If OriginalProtection <> wdNoProtection Then
only tests for ONE value (-1, or wdNoProtection), and then based on that, at the end MAKES ActiveDocument.ProtectionType = 2 (wdAllowOnlyFormFields).

This will work, of course, but it is NOT a good use of explicit logic and values. I know this is fussing, but technically it seeems to me that you should return the document back to its original value...whatever it was.

If you are only want to test for wdAllowOnlyFormFields, then THAT is what you should test for. This is the use of explicit values.
Code:
If OriginalProtection [b][COLOR=red]= wdAllowOnlyFormFields[/color red][/b] Then 
etc etc
Again, I know, I know this is fussing. But say, by accident, someone protected for Comments. And suppose they also added a password to that.
Indulge me here.
Code:
If OriginalProtection <> wdNoProtection Then
[COLOR=red]' this would return TRUE, it is NOT NoProtection[/color red]
ActiveDocument.Unprotect
[COLOR=red]' would fail as no password included.[/color red]
OK, suppose, they did NOT put a password on it. Fine, then the code would roll along, and the document would become unprotected. Cool. Then it gets to the end and the document is protected for forms. The concept was to RE-protect, but in this (admittedly stretched example), it is not RE-protected. It is a new protection, AND not only that, but suppose the original document had different sections? Some protected, some not.

If it WAS originally protected for forms, then protected it again would reinstate the appropriate sections. However, if it was NOT originally protected for forms (back to my accidental protected for Comments), then protecting it for forms would - by default - protect ALL Sections.

Again, I am fussing. I try to have a thing for being explicit.

Test for what you want to (explicitly) test for.

If you are returning things to original states, then return them to what they explicitly were.

No sleep. Ranting......

Gerry
My paintings and sculpture
 
Luckily, I think we will only ever encounter docs with "AllowOnlyFormFields"
so I’ve used your "If OriginalProtection = wdAllowOnlyFormFields Then etc etc" code. Thanks for noticing that.

However, to be finicky, this would be best if it covered all types of protection and if it couldn't carry out the macro because, say, the doc was protected by a password, I guess it should at least return an error message to the user stating why it cannot carry on.

Any pointers on how to include these scenarios would be great.

Thanks
 
Hi Gerry,

> technically it seeems to me that you should return the document back to its original value...whatever it was.

That is what my original post suggested :)

For the record, WdProtectionType also has a value of 3 (wdAllowOnlyReading) in Word 2003 - the OP's version. There is probably a case for version-dependent code here.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
Hey Tony.

Your original post did cover this with "OriginalProtection" but the problem was that form fields would reset and display their code rather than the data that was in them originally. This is why I had to empoy:
Code:
Type:=wdAllowOnlyFormFields, NoReset:=True

Thanks
 
Yes Tony, you did with
Code:
If OriginalProtection <> wdNoProtection Then
ActiveDocument.Protect OriginalProtection
which of course DOES return it to the original value. My note was not directed to you....heaven forbid!

dotobi -
Your original post did cover this with "OriginalProtection" but the problem was that form fields would reset and display their code rather than the data that was in them originally. This is why I had to employ:
Not quite correct. Tony's original code
Code:
ActiveDocument.Protect OriginalProtection
it is true will reset. However, OriginalProtection is just a number, so you can use it with Type:=, AND NoReSet:
Code:
ActiveDocument.Protect Type:=OriginalProtection, NoReset:=True
which will return the protection to its original value (regardless of what that it is), and not reset the formfields.
say, the doc was protected by a password, I guess it should at least return an error message to the user stating why it cannot carry on.
It does return an error, per se. It displays the inputbox for the password.

Gerry
My paintings and sculpture
 
ahhh-haaa. Great. Now everything is covered. Tested it. Working brilliantly. Thankyou everyone :)

THE FINAL CODE:
Code:
Sub printLHPL4all()
Dim strPrinter As String
strPrinter = Application.ActivePrinter
Application.WordBasic.FilePrintSetup Printer:="\\[i]servername[/i]\[i]printername[/i] on [i]portname[/i]", _
    DoNotSetAsSysDefault:=1
Dim OriginalProtection As WdProtectionType
OriginalProtection = ActiveDocument.ProtectionType
If OriginalProtection <> wdNoProtection Then ActiveDocument.Unprotect
    With ActiveDocument.PageSetup
        .FirstPageTray = wdPrinterLowerBin
        .OtherPagesTray = wdPrinterPaperCassette
    End With
    ActiveDocument.PrintOut
    With ActiveDocument.PageSetup
        .FirstPageTray = wdPrinterPaperCassette
        .OtherPagesTray = wdPrinterPaperCassette
    End With
    ActiveDocument.PrintOut
Application.ActivePrinter = strPrinter
If OriginalProtection <> wdNoProtection Then ActiveDocument.Protect Type:=OriginalProtection, NoReset:=True
End Sub

Thanks

keV
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top