Hello,
I have been trying different codes and suggestions I have found online but still having a problem. I have a mail merge that I run and would like to then run a macro that will divide the applications up into individual PDF files. So far I've been able to
get the mail merge to save as 1 single PDF instead of separate PDF documents. Each document should contain 2 pages. This is the code I have thus far:
Sub Save_Merged_As_PDFs2()
'Makes the code run faster and reduces screen flicker a bit.
Application.ScreenUpdating = False
' Select a folder, change the default file save location below so it's not just C:\
Dim strFolder As String
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
With fd
.Title = "Select the folder into which the documents will be saved."
If .Show = -1 Then
strFolder = .SelectedItems(1) & ""
Else
MsgBox "The documents will be saved in the default document file location."
strFolder = "S:\Readmit\Continuation Appeal"
End If
End With
ChangeFileOpenDirectory strFolder
Dim DokName As String 'ADDED CODE
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
.LastRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord + .RecordCount
' Remember the wanted documentname
DokName = .DataFields("Username").Value ' ADDED CODE
End With
' Merge the active record
.Execute Pause:=False
End With
'Used to set criteria for moving through the document by section.
Application.Browser.Target = wdBrowseSection
'A mailmerge document ends with a section break next page.
'Subtracting one from the section count stop error message.
For i = 1 To ((ActiveDocument.Sections.Count) - 1)
'Select and copy the section text to the clipboard
ActiveDocument.Bookmarks("\Section").Range.Copy
'Create a new document to paste text from clipboard.
Documents.Add
Selection.PasteAndFormat (wdFormatOriginalFormatting)
'Removes the break that is copied at the end of the section, if any.
Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
' Save then resulting document. NOTICE MODIFIED filename
ActiveDocument.ExportAsFixedFormat OutputFileName:="S:\Readmit\Continuation Appeal" + DokName + ".pdf", _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
ActiveDocument.Close savechanges:=wdDoNotSaveChanges
'Move the selection to the next section in the document
Application.Browser.Next
Next i
ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Sub
Any help is greatly appreciated. Thanks in advance!