Consulting

Results 1 to 18 of 18

Thread: Unprotect a document with a macro?

  1. #1

    Question Unprotect a document with a macro?

    HI,

    i am wondering if it is possible to create a macro that unprotects a protected document?

    i understand that it is not possible to do this using the record new macro tool and simply clicking on it and saving the macro but i am wo9ndering if there is any code that can be manually put in to achieve this?

    regards

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    ActiveDocument.Unprotect Password:="pwd"
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    thanks alot worked a treat


  4. #4

    Question

    another question,

    is there any way to say if the document is protected, unprotected it, then if the document is unprotected then ignore the unprotec piece of code.

    my situation is i have a print macro that allows me to print 2 copies of the same page in word. but my word documents are protected when i first open them.

    what i do is, i open the word document and enter the relevant information and then click the print macro which prints the document twice and unprotects the document.

    then i change a piece of information and want to print it again using the same macro but this time ignoring the unprotecting piece of code that you gave me (which is spot on by the way) because it throws an error which i understand.

    i hope you understand my situation

    cheers

  5. #5
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Like this:
     
    Sub leaveunprotected()
            If ActiveDocument.ProtectionType = wdNoProtection Then
        MsgBox "test"   'your code here
            Else
        ActiveDocument.Unprotect Password:="pwd"
        End If
    End Sub
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  6. #6
    much appriciated,

    works a treat,

    is there any way to not have the message box and to skip this part and just go ahead and print the two documents?

    regards

  7. #7
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    This may be better:
     
    Sub leaveunprotected() 
        If ActiveDocument.ProtectionType = wdNoProtection Then 
            Else 
        ActiveDocument.Unprotect Password:="pwd" 
        End If 
          MsgBox "test" 'your code here
    End Sub
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  8. #8
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    try replacing the msgbox code in post #7 with your print code....

    If that don't work maybe you could post the document....
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  9. #9
    all sorted,

    the second code you sent i just deleted the message box part and it does exactally what i want

    many thanks again

  10. #10
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Good deal Phill, be sure to mark your thread solved using thread tools at the top of the page.

    That way others looking to help won't needlessly visit the page.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  11. #11
    thanks for the tip

  12. #12
    VBAX Newbie
    Joined
    May 2010
    Posts
    5
    Location

    How to hide the actual password in the macro?

    This macro worked great for me as I want to be able to provide clients with the option of unprotecting a form should they need to make changes to contract terms. However, how do you prevent them from double clicking on the command button to see the underlying code, where the actual password is typed? Definitely don't want them to know the actual password. Thanks.

  13. #13
    VBAX Newbie
    Joined
    May 2010
    Posts
    5
    Location

    How to unprotect for tracked changes only

    Hi -

    In addition to my question above, does anyone have code that will not only unprotect the document, but ''re-protect'' it for tracked changes mode only? While I want to be client friendly and allow clients to make changes to terms, I also want the changes to be visible in tracked changes. Unfortunately, some clients have been known to sneak in changes and then sign the document without having us review their changes.

    Since Word cannot allow two forms of a protection in a document, maybe there is code to allow this. Thank you.

  14. #14
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    True, you can not set two types of protection.

    ActiveDocument.Protect(wdAllowOnlyRevisions) allows...only revisions.

    If you want to have them be able to make changes, then you can NOT have it protected for forms, and vice versa.

    You can switch between the protection types though. So you could turn OFF protection for forms, then turn ON protection for revisions.

    "However, how do you prevent them from double clicking on the command button to see the underlying code, where the actual password is typed? "

    I am not following. Do you mean going into the Visual Basic Editor and looking at the code (assuming the turning off protection code has a password)? You can't. Once you turn off protection, it is turned off.

    "I want to be able to provide clients with the option of unprotecting a form should they need to make changes to contract terms"

    In which case...what is the point of having protection in the first place???

  15. #15
    VBAX Newbie
    Joined
    May 2010
    Posts
    5
    Location
    Thank you. How do you allow a user to switch from form mode to revision mode?

    The reason for starting in forms mode is to make completion easier for my company's users (and to prevent our teams from making revisions). The reason for wanting to allow revisions mode is to allow our clients to make changes. Most clients do not like to receive a form that they cannot edit, and they then have to come back and ask for an unprotected form. I'm trying to balance controls with flexibility. Thanks.

  16. #16
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    With ActiveDocument
       .Unprotect "xxxxx"
       .Protect wdAllowOnlyRevisions, , Password:="xxxxx"
    End With
    turns OFF whatever protection type it was, and turns ON protection for revisions.

    "I'm trying to balance controls with flexibility."

    In a way, you have an impossible task.

  17. #17
    VBAX Newbie
    Joined
    May 2010
    Posts
    5
    Location
    Thank you Gerry, this has been a helpful solution.

    When you're dealing with contracts in the industry we are in, we have to have both controls and flexibility. Unfortunately we don't have a fancy contract management system with sophisticated document authoring and creation capabilities. Thanks again.

  18. #18
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    All I can say is that I hope you never, ever, open documents from email attachments.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •