Old Code needs updating - Password Protection

Crosby87

New Member
Joined
Oct 6, 2009
Messages
42
Hi All,

We have some old code that we use to basically create a mail merge from a data set as individual letters.

I didn't write the code but i need to find away to password protect the each file with a specific password for example say Field1&Field2. Could anyone help with this?

Code:
Sub lettermaker()
Application.ScreenUpdating = False
Dim StrPath As String, StrName As String, MainDoc As Document
Application.ScreenUpdating = False
Set MainDoc = ActiveDocument

With MainDoc
  For i = 1 To .MailMerge.DataSource.RecordCount
    With .MailMerge
      .Destination = wdSendToNewDocument
      .SuppressBlankLines = True
      With .DataSource
        .FirstRecord = i
        .LastRecord = i
        .ActiveRecord = i
        StrName = .DataFields("LetterName")
        StrPath = "C:\Users\Craig.Crosby\Desktop\Letters\2017\" & .DataFields("Folder")\" & .DataFields("BP")& "\"
If Len(Dir(StrPath, vbDirectory)) = 0 Then
   MkDir StrPath
End If
      End With
      .Execute Pause:=False
    End With
    With ActiveDocument
   '.SaveAs2 FileName:=StrPath & StrName & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
      '.SaveAs FileName:=StrPath & StrName & ".doc", FileFormat:=wdFormatDocument, AddToRecentFiles:=False
            .ExportAsFixedFormat OutputFileName:=StrPath & StrName & ".pdf", ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:=wdExportOptimizeForPrint, Range:=wdExportAllDocument, _
        Item:=wdExportDocumentContent, IncludeDocProps:=True, CreateBookmarks:=wdExportCreateNoBookmarks, _
        KeepIRM:=True, DocStructureTags:=True, BitmapMissingFonts:=True, UseISO19005_1:=False
      .Close SaveChanges:=False
      
    End With
  Next i
End With
Application.ScreenUpdating = False
End Sub


Thanks in advance
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Although Word allows you to add a password to a doc or docx file, that isn't supported for documents saved as PDFs, whether via .SaveAs or .ExportAsFixedFormat.
 
Upvote 0
Thanks for the quick reply (sorry for posting to the wrong page also). The code has an option to save as a word document as well as a PDF, the Word document is the option i want to use.
 
Upvote 0
Since: (a) you made no mention of saving as a document; (b) your code has the document-saving code commented-out; and (c) only the PDF-generation code is active, it wasn't apparent you wanted anything other than a PDF solution.

To password-protect a document, add another variable:
StrPwd As String
which you populate via:
StrPwd = .DataFields("Field1") & .DataFields("Field1")
within your 'With .DataSource ... End With' block, where 'Field1' & 'Field2' are the relevant field names; and using, for example:
.SaveAs2 FileName:=StrPath & StrName & ".docx", FileFormat:=wdFormatXMLDocument, Password:=StrPwd, AddToRecentFiles:=False
 
Upvote 0

Forum statistics

Threads
1,215,157
Messages
6,123,341
Members
449,097
Latest member
thnirmitha

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top