Smart questions
Smart answers
Smart people
Join Tek-Tips Forums
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Use ActiveDocument.Unprotect only if document is protectedHelpful Member!(2) 

dotobi (TechnicalUser) (OP)
10 Jul 06 13:38
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 (http://www.tek-tips.com/viewthread.cfm?qid=1252274&page=1).

Thanks

Kev
PHV (MIS)
10 Jul 06 13:40
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

dotobi (TechnicalUser) (OP)
10 Jul 06 14:01
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
PHV (MIS)
10 Jul 06 14:10
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

Helpful Member!  TonyJollans (Programmer)
11 Jul 06 6:18

> 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

Dim OriginalProtection As WdProtectionType

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


' Later
If OriginalProtection <> wdNoProtection Then ActiveDocument.Protect OriginalProtection
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

dotobi (TechnicalUser) (OP)
11 Jul 06 7:30
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
dotobi (TechnicalUser) (OP)
11 Jul 06 7:37
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
Helpful Member!  fumei (TechnicalUser)
11 Jul 06 8:53
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 = wdAllowOnlyFormFields 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
' this would return TRUE, it is NOT NoProtection
ActiveDocument.Unprotect
' would fail as no password included.
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

dotobi (TechnicalUser) (OP)
11 Jul 06 9:13
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
TonyJollans (Programmer)
11 Jul 06 9:18
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

dotobi (TechnicalUser) (OP)
11 Jul 06 9:41
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
fumei (TechnicalUser)
11 Jul 06 10:18
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 -

Quote:

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.

Quote:

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

dotobi (TechnicalUser) (OP)
11 Jul 06 12:17
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:="\\servername\printername on portname", _
    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

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close