Macro to protect/unprotect document

Discussion in 'Word VBA Beginners' started by Ricki Miles, Sep 16, 2009.

  1. Ricki Miles

    Ricki Miles Guest

    I am creating a template in Word 2003 that is protected for form fields. I
    would like a macro to unprotect the form (retaining the data in the form
    fields) so that users can edit the footer. I would like another macro to
    reprotect the form. I currently have a macro for each that will unprotect
    and reprotect, but if the document is protected and the protect macro is
    run, an error occurs. Similarly, if the document is unprotected and the
    unprotect macro is run, an error occurs as well. How can I change the VBA
    code so that if the document is already protected and the protect macro is
    run, nothing happens and the same for unprotect. Below is the code I was
    using. Thanks,

    Ricki

    If ActiveDocument.ProtectionType = wdNoProtection Then
    ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
    Else
    ActiveDocument.Unprotect
    End If
     
    Ricki Miles, Sep 16, 2009
    #1
    1. Advertisements

  2. Ricki Miles

    Graham Mayor Guest

    Why not simply use the lock button from the forms toolbar?

    'Unprotect the file
    If ActiveDocument.ProtectionType <> wdNoProtection Then
    ActiveDocument.Unprotect Password:=""
    End If

    'Reprotect the document.
    If ActiveDocument.ProtectionType = wdNoProtection Then
    ActiveDocument.Protect _
    Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
    End If

    --
    <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
    Graham Mayor - Word MVP


    <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
     
    Graham Mayor, Sep 16, 2009
    #2
    1. Advertisements

  3. Ricki Miles

    Ricki Miles Guest

    Hi Graham,

    Thanks for the suggestion - it works!

    Ricki
     
    Ricki Miles, Sep 16, 2009
    #3
  4. Ricki Miles

    iris b Guest

    Hi Graham,

    I saw your reply to ricki... I need to add some more code to this...

    I need to unprotect a word document with these steps:

    1. create a message to the user "enter password"
    2. if password is correct then - unprotect
    end if
    3. if password is not correct then
    create a messeage: "uncorrect password"
    end if

    I dont know how to prompt the message and use it's text box for "true" or "false" unswers.

    Thank you in advance

    iris
     
    iris b, Oct 27, 2011
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.