Consulting

Results 1 to 2 of 2

Thread: Code to: un-protect w/password, attach document, re-protect with password

  1. #1
    VBAX Newbie
    Joined
    Sep 2019
    Posts
    1
    Location

    Code to: un-protect w/password, attach document, re-protect with password

    Good Morning! 🌞

    I have the code set up for the attach button, but it doesn't function properly when the form is password protected. This is the code I have:

    Private Sub CommandButton2_Click()' Browse & Select File
    With Application.FileDialog(msoFileDialogFilePicker)
            .AllowMultiSelect = False
            .Title = "Select the File that you want to insert"
            If .Show = True Then
                FiletoInsert = .SelectedItems(1)
            Else
                Exit Sub
            End If
        End With
    End Sub
    I need to add code to un-protect the document with a password and re-protect it once the attachment is attached. I've also added to a screenshot to show my protection settings.

    Can anyone assist?

    Thank you!
    Attached Images Attached Images

  2. #2
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    252
    Location
    Try this.

    Private Sub CommandButton2_Click() ' Browse & Select File
    strPassword = "your password here"
    If ActiveDocument.ProtectionType <> wdNoProtection Then
    ActiveDocument.Unprotect Password:=strPassword
    End If
    With Application.FileDialog(msoFileDialogFilePicker)
            .AllowMultiSelect = False
            .Title = "Select the File that you want to insert"
            If .Show = True Then
                FiletoInsert = .SelectedItems(1)
            Else
                Exit Sub
            End If
    End With
    Application.ActiveDocument.Protect wdAllowOnlyFormFields, Password:=strPassword
    End Sub

Posting Permissions

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