{"id":412,"date":"2023-03-25T11:15:21","date_gmt":"2023-03-25T11:15:21","guid":{"rendered":"https:\/\/xuan.co.uk\/?p=412"},"modified":"2023-07-17T20:54:55","modified_gmt":"2023-07-17T20:54:55","slug":"save-individual-files-from-mailmerge","status":"publish","type":"post","link":"https:\/\/xuan.co.uk\/?p=412","title":{"rendered":"Save individual files from mailmerge"},"content":{"rendered":"<p>For the macro to work, you will need to add in four extra columns named: DocFolderPath, DocFileName, PdfFolderPath, and PdfFileName. These should be exactly as written here, with no spaces and each word capitalised.<\/p>\n<p>Sub MailMergeToPdfBasic() &#8216; Mark the start of the Subroutine (i.e. Macro) and name it &#8220;MailMergeToPdf&#8221;<br \/>\n&#8216; Macro created by Imnoss Ltd<br \/>\n&#8216; Please share freely while retaining attribution<br \/>\n&#8216; Last Updated 2021-05-03<br \/>\nDim masterDoc As Document, singleDoc As Document, lastRecordNum As Long &#8216; Create variables (&#8220;Post-it Notes&#8221;) for later use<br \/>\nSet masterDoc = ActiveDocument &#8216; Identify the ActiveDocument (foremost doc when Macro run) as &#8220;masterDoc&#8221;<\/p>\n<p>masterDoc.MailMerge.DataSource.ActiveRecord = wdLastRecord &#8216; jump to the last active record (active = ticked in edit recipients)<br \/>\nlastRecordNum = masterDoc.MailMerge.DataSource.ActiveRecord &#8216; retrieve the record number of the last active record so we know when to stop<\/p>\n<p>masterDoc.MailMerge.DataSource.ActiveRecord = wdFirstRecord &#8216; jump to the first active record (active = ticked in edit recipients)<br \/>\nDo While lastRecordNum &gt; 0 &#8216; create a loop, lastRecordNum is used to end the loop by setting to zero (see below)<br \/>\nmasterDoc.MailMerge.Destination = wdSendToNewDocument &#8216; Identify that we are creating a word docx (and no e.g. an email)<br \/>\nmasterDoc.MailMerge.DataSource.FirstRecord = masterDoc.MailMerge.DataSource.ActiveRecord &#8216; Limit the selection to just one document by setting the start &#8230;<br \/>\nmasterDoc.MailMerge.DataSource.LastRecord = masterDoc.MailMerge.DataSource.ActiveRecord &#8216; &#8230; and end points to the active record<br \/>\nmasterDoc.MailMerge.Execute False &#8216; run the MailMerge based on the above settings (i.e. for one record)<br \/>\nSet singleDoc = ActiveDocument &#8216; Identify the ActiveDocument (foremost doc after running the MailMerge) as &#8220;singleDoc&#8221;<br \/>\nsingleDoc.SaveAs2 _<br \/>\nFileName:=masterDoc.MailMerge.DataSource.DataFields(&#8220;DocFolderPath&#8221;).Value &amp; Application.PathSeparator &amp; _<br \/>\nmasterDoc.MailMerge.DataSource.DataFields(&#8220;DocFileName&#8221;).Value &amp; &#8220;.docx&#8221;, _<br \/>\nFileFormat:=wdFormatXMLDocument &#8216; Save &#8220;singleDoc&#8221; as a word docx with the details provided in the DocFolderPath and DocFileName fields in the MailMerge data<br \/>\nsingleDoc.ExportAsFixedFormat _<br \/>\nOutputFileName:=masterDoc.MailMerge.DataSource.DataFields(&#8220;PdfFolderPath&#8221;).Value &amp; Application.PathSeparator &amp; _<br \/>\nmasterDoc.MailMerge.DataSource.DataFields(&#8220;PdfFileName&#8221;).Value &amp; &#8220;.pdf&#8221;, _<br \/>\nExportFormat:=wdExportFormatPDF &#8216; Export &#8220;singleDoc&#8221; as a PDF with the details provided in the PdfFolderPath and PdfFileName fields in the MailMerge data<br \/>\nsingleDoc.Close False &#8216; Close &#8220;singleDoc&#8221;, the variable &#8220;singleDoc&#8221; can now be used for the next record when created<br \/>\nIf masterDoc.MailMerge.DataSource.ActiveRecord &gt;= lastRecordNum Then &#8216; test if we have just created a document for the last record<br \/>\nlastRecordNum = 0 &#8216; if so we set lastRecordNum to zero to indicate that the loop should end<br \/>\nElse<br \/>\nmasterDoc.MailMerge.DataSource.ActiveRecord = wdNextRecord &#8216; otherwise go to the next active record<br \/>\nEnd If<\/p>\n<p>Loop &#8216; loop back to the Do start<br \/>\nEnd Sub &#8216; Mark the end of the Subroutine<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For the macro to work, you will need to add in four extra columns named: DocFolderPath, DocFileName, PdfFolderPath, and PdfFileName. These should be exactly as written here, with no spaces and each word capitalised. Sub MailMergeToPdfBasic() &#8216; Mark the start of the Subroutine (i.e. Macro) and name it &#8220;MailMergeToPdf&#8221; &#8216; <a class=\"more-link\" href=\"https:\/\/xuan.co.uk\/?p=412\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[83],"class_list":["post-412","post","type-post","status-publish","format-standard","hentry","category-tech-tips","tag-mailmerge-individual"],"_links":{"self":[{"href":"https:\/\/xuan.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/412","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xuan.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xuan.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xuan.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xuan.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=412"}],"version-history":[{"count":1,"href":"https:\/\/xuan.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/412\/revisions"}],"predecessor-version":[{"id":413,"href":"https:\/\/xuan.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/412\/revisions\/413"}],"wp:attachment":[{"href":"https:\/\/xuan.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xuan.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xuan.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}