Results 1 to 4 of 4
  1. #1
    Star Lounger
    Join Date
    May 2009
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to unlock form with two possible passwords and flag user if neither is correct?

    My users will receive a Word 2010 form by email. Either it will be locked with a password preset by me or it could just be locked for filling in the fields (preparing for either event) or it could be locked with a password unknown to me. The user clicks a toolbar button to start the routine.

    1 If the form does not have a password and it is locked for form fields, unlock form.
    2 If the form contains the password set by me, unlock the form.
    3 If the form contains an unknown password set by another person, show a message box.

    In the case of #3, if there is code to also display the Remove password dialog box, please list that. The users are new to Word 2010 and doubtful they would know to click the Review tab and click the Restrict Editing button.

    I am having problems covering the three events and this is my attempt at coding. Where have I gone wrong? Thank you for your help!

    Sub UnprotectDoc()

    On Error GoTo Errorhandler

    If ActiveDocument.ProtectionType = wdNoProtection Then

    Else

    `This is my attempt to check for the known password and the lock form for
    `form filling action of "" - I'm sure there is a better method - Charlie

    ActiveDocument.Unprotect Password:="45$123" Or ""

    End If

    Errorhandler:

    MsgBox "Contact the sender for the password."

    End Sub

    Thank you for your help!
    Charlie
    charlie6067

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,433
    Thanks
    371
    Thanked 1,456 Times in 1,325 Posts
    Charlie,

    I'm not up on protected forms but from looking at the code I see these problems:
    1. ActiveDocument.Unprotect Password:="45$123" does not support an OR operator you must provide one password.
    2. You haven't prompted for a password:
      Code:
      zPswd = Inputbox("Please provide a password to unlock the form?", _
                   "Password Entry")
      ActiveDocument.Unprotect Password:=zPswd
    3. You're emailing the document but you have no way to insure that the reciepient places the document in a "Trusted Location" so the code will be allowed to run.
    4. You should probably have the code in an Document_Activate event so it fires off automatically.

    HTH
    Last edited by RetiredGeek; 2014-10-19 at 15:35.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  3. #3
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,433
    Thanks
    371
    Thanked 1,456 Times in 1,325 Posts
    Charlie,

    Here's some working code you can play with:
    Code:
    Option Explicit
    
    Sub ProtectDoc()
    '
    ' ProtwctDoc Macro
    '
    '
        Selection.Editors.Add wdEditorEveryone
        ActiveDocument.Protect Password:="Test", NoReset:=False, Type:= _
            wdAllowOnlyReading, UseIRM:=False, EnforceStyleLock:=False
            
    End Sub      'ProtectDoc
    
    Sub UnprotectDoc()
    
    ' UnprotectDoc Macro
       
       Dim zPswd As String
       On Error GoTo InvalidPassword
       
       zPswd = InputBox("Please provide a password to unlock the form?", _
                        "Password Entry")
       ActiveDocument.Unprotect Password:=zPswd
       
       Exit Sub
       
    InvalidPassword:
    
      If Err = 5485 Then
        MsgBox "The password supplied: " & _
               "" & " is invalid!" & vbCrLf & vbCrLf & _
               "Please obtain the correct passwors and try again.", _
               vbCritical + vbOKOnly, _
               "File Editing Access Denied!"
      Else
          MsgBox "Unexpected error: " & _
               Format(Err) & " please contact the programmer!", _
               vbCritical + vbOKOnly, _
               "Untrapped Erroe Encountered"
    
      End If
    
    End Sub    'UnprotectDoc
    HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  4. #4
    Star Lounger
    Join Date
    May 2009
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you very much Retired Geek - I appreciate your quick response and code.
    Charlie
    charlie6067

Posting Permissions

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