View Single Post
 
Old 01-04-2021, 02:57 PM
Angel21 Angel21 is offline Windows Vista Office 2010
Novice
 
Join Date: Jan 2021
Posts: 1
Angel21 is on a distinguished road
Default Mail Merge Docs Save as pdf - Code in VBA

Quote:
Originally Posted by macropod View Post
To create the individual PDFs, you could drive the merge with a macro like:
Code:
Sub Merge_To_Individual_Files()
'Merges one record at a time to the chosen output folder
Application.ScreenUpdating = False
Dim StrFolder As String, StrName As String, MainDoc As Document, i As Long, j as long
Set MainDoc = ActiveDocument
With MainDoc
  StrFolder = .Path & "\"
  For i = 1 To .MailMerge.DataSource.RecordCount
    With .MailMerge
      .Destination = wdSendToNewDocument
      .SuppressBlankLines = True
      With .DataSource
        .FirstRecord = i
        .LastRecord = i
        .ActiveRecord = i
        If Trim(.DataFields("Last_Name")) = "" Then Exit For
        StrName = .DataFields("Last_Name") & "_" & .DataFields("First_Name")
      End With
      .Execute Pause:=False
    End With
    For j = 1 To 255
      Select Case j
        Case 1 To 31, 33 To 45, 47, 58 To 64, 91 To 94, 96, 123 To 141, 143 To 149, 152 To 157, 160 To 180, 182 To 191
        StrName = Replace(StrName, Chr(j), "")
      End Select
    Next
    StrName = Trim(StrName)
    With ActiveDocument
      .SaveAs2 FileName:=StrPath & StrName & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
      .Close SaveChanges:=False
    End With
  Next i
End With
Application.ScreenUpdating = True
End Sub
Note: the above macro is coded to get the filenames from two of the data fields, via the line:
StrName = .DataFields("Last_Name") & "_" & .DataFields("First_Name")
You will need to change that lines to reference the data field(s) you want to use.
The output PDFs will be saved in the same folder you keep your mailmerge main document in.
Hi

Please can you let me how to change the code to save the pdfs file in a location of my choice

Thanks