Mailmerge Selected Range to Individual PDFs

N Prakash

Active Member
Joined
Nov 7, 2003
Messages
409
Try:
Code:
Sub merge1record_at_a_time()
Application.ScreenUpdating = False
Dim StrPath As String, StrName As String, MainDoc As Document
StrPath = "C:\File path\"
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("ID")
      End With
      .Execute Pause:=False
    End With
    With ActiveDocument
      .SaveAs2 FileName:=StrPath & StrName & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
      .Close SaveChanges:=False
    End With
  Next i
End With
Application.ScreenUpdating = True
End Sub
Naturally, you'll need to use your own path for the StrPath variable and, if you're not saving docx files, change the '.SaveAs2' line to:
Code:
      .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

Dear Macropad,

I used the above code to generate .pdf file and it is working perfectly well. For my testing I created 10 .pdf files and it worked as expected. But this has also created 37 word document I do not know how but I need to delete each word file one by one and it is not even allowing me to close the word application. Then from Task manager I had to select all word files and kill the process. Will you please help me the word document created should get closed without intervention and my .pdf files alone should be in the folder.

Regards,

Prakash
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Re: Word 2007/2010 Mail Merge to save to individual PDF files

you maybe able to just REM out the following
Code:
    With ActiveDocument
      .SaveAs2 FileName:=StrPath & StrName & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
      .Close SaveChanges:=False
    End With
which from first look appears to create Word files
 
Upvote 0
Re: Word 2007/2010 Mail Merge to save to individual PDF files

you maybe able to just REM out the following
Code:
    With ActiveDocument
      .SaveAs2 FileName:=StrPath & StrName & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
      .Close SaveChanges:=False
    End With
which from first look appears to create Word files
No, that won't work. You need all of:
Code:
    With ActiveDocument

      .Close SaveChanges:=False
    End With
 
Upvote 0
Re: Word 2007/2010 Mail Merge to save to individual PDF files

Thank you all for all the help. in fact the corrected code is mentioned above itself and I had not seen this. This line is corrected as follows;

.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
ActiveWindow.Close SaveChanges:=False
 
Upvote 0
Re: Word 2007/2010 Mail Merge to save to individual PDF files

My another requirement is while generating the PDF file system should prompt for the Sl No. from and to so that selected .pdf file alone can be generated. Sl.No is one the data field. Any help please.
 
Upvote 0
Re: Word 2007/2010 Mail Merge to save to individual PDF files

if its listed you could add SI into the structure that builds StrName

StrName = strFolder & sheets("sheetname").range("SI holder cell") & Trim(.DataFields("Name")) & "-" & Format(.DataFields("Date"), "YYYY_MM_DD")
 
Upvote 0
Re: Word 2007/2010 Mail Merge to save to individual PDF files

I need a screen to popup to input From and To sl. no so that only those .pdf files are created and stored in the designated folder. This is because one day I I may create 10 pdf file and mail it using automatic creation of mail by using a macro code in outlook. Next day I may decide to send from, 11 to 25 etc.
 
Upvote 0
Re: Word 2007/2010 Mail Merge to save to individual PDF files

Thank you all for all the help. in fact the corrected code is mentioned above itself and I had not seen this. This line is corrected as follows;

.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
ActiveWindow.Close SaveChanges:=False
Actually, the 'correct' code, as per post #1 in this thread, is:
Code:
    With ActiveDocument
      .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
or, alternatively:
Code:
    With ActiveDocument
      .SaveAs FileName:=StrPath & StrName & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
      .Close SaveChanges:=False
    End With
My another requirement is while generating the PDF file system should prompt for the Sl No. from and to so that selected .pdf file alone can be generated. Sl.No is one the data field. Any help please.
Does this mean you only want to output a single record? If so, try:
Code:
Sub merge1record_at_a_time()
Application.ScreenUpdating = False
Dim StrPath As String, StrName As String, MainDoc As Document, strRcrd As String, bRslt As Boolean
StrPath = "C:\File path\"
strRcrd = Trim(InputBox("Which Sl No. to merge?"))
If strRcrd = "" Then Exit Sub
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("ID")
      End With
      bRslt = False
      If .DataFields("Sl_No") = strRcrd Then
      .Execute Pause:=False
      bRslt = True
      End If
    End With
    If bRslt = True Then
      With ActiveDocument
        .SaveAs FileName:=StrPath & StrName & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
        .Close SaveChanges:=False
      End With
    End If
  Next i
End With
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Re: Word 2007/2010 Mail Merge to save to individual PDF files

I want to out put multiple records depending on the Sl_No. In the followings line error is being returned "Compilation error: Method or data member not found"

If .DataFields("Sl_No") = strRcrd Then

Data field Sl_No is defined in the Excel file
Sl_No
1
2
3
4
5
6
7
8
9
10
11
12
13
 
Upvote 0

Forum statistics

Threads
1,226,121
Messages
6,189,089
Members
453,524
Latest member
AshJames

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