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


    <>>< ><<> ><<> <>>< ><<> <>>< <>><<>

    Ricki Miles wrote:
    > 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
     
    Graham Mayor, Sep 16, 2009
    #2
    1. Advertisements

  3. Ricki Miles

    Ricki Miles Guest

    Hi Graham,

    Thanks for the suggestion - it works!

    Ricki
    "Graham Mayor" <> wrote in message
    news:...
    > 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
    >

    > <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
    >
    > Ricki Miles wrote:
    >> 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
    #3
  4. Ricki Miles

    iris b Guest

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

    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

    > On Wednesday, September 16, 2009 9:41 AM Ricki Miles wrote:


    > 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



    >> On Wednesday, September 16, 2009 10:11 AM Graham Mayor wrote:


    >> 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
    >>

    >> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
    >>
    >> Ricki Miles wrote:



    >>> On Wednesday, September 16, 2009 11:11 AM Ricki Miles wrote:


    >>> Hi Graham,
    >>>
    >>> Thanks for the suggestion - it works!
    >>>
    >>> Ricki
     
    iris b, Oct 27, 2011
    #4
    1. Advertisements

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.
Similar Threads
  1. Ricki Miles

    VBA language to Protect Document

    Ricki Miles, Sep 16, 2003, in forum: Word VBA Beginners
    Replies:
    2
    Views:
    139
  2. Nik Bedwell
    Replies:
    2
    Views:
    518
    Nik Bedwell
    Sep 19, 2003
  3. Micayla

    protect document in macro

    Micayla, Nov 20, 2003, in forum: Word VBA Beginners
    Replies:
    3
    Views:
    102
    Doug Robbins - Word MVP
    Nov 21, 2003
  4. Laura Ashley

    Unprotect a Section of a Table

    Laura Ashley, Feb 11, 2004, in forum: Word VBA Beginners
    Replies:
    6
    Views:
    159
    Laura Ashley
    Feb 12, 2004
  5. sandy

    Protect document containing text boxes

    sandy, May 9, 2004, in forum: Word VBA Beginners
    Replies:
    1
    Views:
    75
    Word Heretic
    May 13, 2004
  6. zSplash

    unprotect VBA project with code

    zSplash, Sep 1, 2004, in forum: Word VBA Beginners
    Replies:
    3
    Views:
    818
    zSplash
    Sep 2, 2004
  7. Jade

    unprotect then protect document...

    Jade, Jun 24, 2007, in forum: Word VBA Beginners
    Replies:
    5
    Views:
    195
    Doug Robbins - Word MVP
    Jun 26, 2007
  8. c_en

    Word Protect/Unprotect Macro for Forms

    c_en, Jun 28, 2009, in forum: Word VBA Beginners
    Replies:
    3
    Views:
    308
Loading...