Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word 2010 - How to prompt a user to set a password for the document

Status
Not open for further replies.

remeng

Technical User
Jul 27, 2006
504
0
16
US
Hi All;

I am almost done programing a document's VBA script. I have 1 last section of code to complete.

Objective:

Check if the document has a password
- if there is a password then sub ends
- if no bring up a box to ask the user if they want to set the password
- if yes then bring up the new password entry box​
- if no end the sub​

The second msgbox will be the box to enter the new password for the document

Current issues:

- When the password is set, the script cannot correctly skip the set password section of the code.
- How do I create a way for the user to enter the new password. I've done significant research and haven't come across an answer.

Code:
Sub passwordcheck_1()

Dim ButtonChosen As Integer
 
If ActiveDocument.HasPassword = False Then

'display message box on screen, with YES and NO buttons

ButtonChosen = MsgBox("This Document is Not Password Protected?", vbYesNo, "Password Not Set")

    If ButtonChosen = 6 Then

MsgBox "This Document is Not Password Protected?", vbOKOnly, "Password Not Set"

    Else
    
    End If

Else

End If

End Sub

Thanks,

Mike
 
Office VBA questions should be asked in forum707
But your MsgBox does not make much sense:[tt]
MsgBox("This Document is Not Password Protected?", vbYesNo, "Password Not Set")[/tt]
This is not a question, it is a statement (with a ? at the end). And how do you expect user to answer either Yes or No to a simple statement?

"This Document is Not Password Protected.
Do you want to set the Password for this document?", vbYesNo - that's a question.



Have fun.

---- Andy

There is a great need for a sarcasm font.
 
current code properly checks if the document is protected or not. The current issue is calling the document lock property and setting the password.

Code:
Sub test()


If ActiveDocument.ProtectionType = wdNoProtection Then

ButtonChosen = MsgBox("This Document is NOT password protected.  Would you like to set the password?", vbYesNo, "Password Not Set")

    If ButtonChosen = vbYes Then

    pass = InputBox("Input New Document Password", "Password")
    
    'ActiveDocument.Protection Type:=wdAllowOnlyFormFields
    
    Application.ScreenUpdating = True
    
    Else


    End If

End If

End Sub
 
Don't you have to enter the new password twice?

Code:
Sub test()
Dim pass1 As String
Dim pass2 As String

If ActiveDocument.ProtectionType = wdNoProtection Then
  If vbYes = MsgBox("This Document is NOT password protected." & vbNewLine & _
   "Would you like to set the password?", vbYesNo, "Password Not Set")

     pass1 = InputBox("Input New Document Password", "Password")
     pass2 = InputBox("Re-Enter your new Password", "Password")
    If pass1 = pass2 Then[green]
        'Some code goes here to set the password [/green]
    End If
    'ActiveDocument.Protection Type:=wdAllowOnlyFormFields
    
    Application.ScreenUpdating = True
  End If
End If

End Sub

Have fun.

---- Andy

There is a great need for a sarcasm font.
 
Your problem might lie in the line
[tt]If ActiveDocument.HasPassword = False Then[/tt]

This looks fine at a passing glance, but [tt]ActiveDocument.HasPassword[/tt] returns a logical result.[ ] If the document does NOT have a password, [tt]ActiveDoccument.HasPassword[/tt] will return the value False, and your line of code becomes
[tt]If False = False Then[/tt]
or, ultimately
[tt]If True Then[/tt]

Change the line to
[tt]If Not ActiveDocument.HasPassword Then[/tt]
(assuming that this is what you actually intend)




 
Hi Andy,

I want to get the equivalent of pass1 working first before I setup the validation.

current code which works but has 1 major issue now. There is a request for the password when the document opens. I need the document limited to form editing only.

Code:
Sub test()

'Dim pass As String

If ActiveDocument.ProtectionType = wdNoProtection Then

ButtonChosen = MsgBox("This Document is NOT password protected.  Would you like to set the password?", vbYesNo, "Password Not Set")

    If ButtonChosen = vbYes Then

    pass = InputBox("Input New Document Password", "Password")
    
 '  mydoc.Password = pass
    

    ActiveDocument.Protect type:=wdAllowOnlyFormFields, Password:=pass
   
    
    
    Application.ScreenUpdating = True
    
    Else


    End If

End If

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top