Home > database >  How to use macros to save a document with password via the "file > save as" dialog?
How to use macros to save a document with password via the "file > save as" dialog?

Time:04-05

I created a Word template with some VBA macro to prompt the user to enter a password. This password should then be used to encrypt the new document created from the template.

The startup code is properly executed and also the user form works as expected. I'm able to "password prompt

Password Code

The code to either set a password only or directly save the document with a password looks like this:

'in TemplateProject > Forms > DialogSetPassword
Private Sub ButtonOnlySetPassword_Click()
    ActiveDocument.password = TextBoxPassword.Text
    MsgBox "Password successfully set"
    'Unload Me
End Sub

Private Sub ButtonSaveWithPassword_Click()
    MsgBox "Password is " & TextBoxPassword.Text  'just for debug reasons to see if the password is properly read
    'ActiveDocument.password = TextBoxPassword.Text  'it works when this line is present, then the password field is pre-filled
    With Application.Dialogs(wdDialogFileSaveAs)
        .Name = ThisDocument.FullName  'name of the template
        .Format = wdFormatXMLDocumentMacroEnabled  'saving as macro enabled document, as there are more macros
        .password = TextBoxPassword.Text
        .Show
    End With
    'Unload Me
End Sub

Private Sub TextBoxPassword_Change()
    If Len(TextBoxPassword.Text) = 0 Then
        ButtonOnlySetPassword.Enabled = False
        ButtonSaveWithPassword.Enabled = False
    Else
        ButtonOnlySetPassword.Enabled = True
        ButtonSaveWithPassword.Enabled = True
    End If
End Sub

According to the documentation, parameters like Name, Format, Password should be accepted by the function, but the password option does not work.

Save Dialog

Even when using the dialog function with the .password parameter, the password field in the save dialog is still empty (can be found under "Save As > Tools > General Options > Password to Open"):

save dialog

CodePudding user response:

The documentation simply lists the parameters of the dialog and gives no details of whether they are read/write or read only. If you use the .Display method, you will find that .Password is read only.

Both the .Display and .Show methods of a dialog return a value, as Long, to indicate the user's action. To use the return value, you call these methods in the same way as for any other function, i.e. .Display() and .Show()

Below is a reworking of your code.

Private Sub ButtonSaveWithPassword_Click()
    Dim ret As Long
    ActiveDocument.Password = TextBoxPassword.Text
    With Application.Dialogs(wdDialogFileSaveAs)
        .Name = ThisDocument.FullName  'name of the template
        .Format = wdFormatXMLDocumentMacroEnabled  'saving as macro enabled document, as there are more macros
        ret = .Show()
        If ret = 0 Then ActiveDocument.Password = vbNullString  'user cancelled save as dialog
    End With
End Sub
  • Related