Login or Sign Up to become a member!
LessThanDot Site Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

Difference between revisions of "Mailmerge Each Document to PDF"

From Wiki

Jump to: navigation, search
(New page: This sample code loops through each mailmerge item in a mailmerge document, opens the item as a letter and saves it to a PDF using a field in the DataSource as a file name. There is no err...)
 
(No difference)

Latest revision as of 10:55, 1 February 2013

This sample code loops through each mailmerge item in a mailmerge document, opens the item as a letter and saves it to a PDF using a field in the DataSource as a file name. There is no error coding and no attempt to check for duplicate file names. It is a snippet.

  1. Dim iRec As Integer
  2. Dim docMail As Document
  3. Dim docLetters As Document
  4.  
  5.  
  6. Set docMail = ActiveDocument
  7.  
  8. ''There is a problem with the recordcount property returning -1
  9. ''http://msdn.microsoft.com/en-us/library/office/ff838901.aspx
  10. docMail.MailMerge.DataSource.ActiveRecord = wdLastRecord
  11. iRec = docMail.MailMerge.DataSource.ActiveRecord
  12.  
  13. docMail.MailMerge.DataSource.ActiveRecord = wdFirstRecord
  14.  
  15. For i = 1 To iRec
  16.     With docMail.MailMerge
  17.         .Destination = wdSendToNewDocument
  18.         .SuppressBlankLines = True
  19.         With .DataSource
  20.             .FirstRecord = i
  21.             .LastRecord = i
  22.                  '' This will be the file name
  23.                 '' the test data source had unique surnames
  24.                 '' in a field (column) called Surname
  25.            sFName = .DataFields("Surname").Value
  26.         End With
  27.         .Execute Pause:=False
  28.         Set docLetters = ActiveDocument
  29.     End With
  30.     docLetters.ExportAsFixedFormat OutputFileName:= _
  31.         "Z:\docs\" & sFName & ".pdf", ExportFormat:= _
  32.         wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
  33.         wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
  34.         Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
  35.         CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
  36.         BitmapMissingFonts:=True, UseISO19005_1:=False
  37.     docLetters.Close False
  38.    
  39.     docMail.MailMerge.DataSource.ActiveRecord = wdNextRecord
  40. Next

842 Rating: 3.2/5 (20 votes cast)