Word 2007/2010 Mail Merge to save to individual PDF files

kryptonian

Board Regular
Joined
Oct 6, 2006
Messages
104
I tried using gmayor's Individual Merge Letters solution but my file only closes without doing anything so I am posting here as a last recourse...

Here's what I want to do --

I'm using Word 2007/2010 mail merge and I want to create individual PDF files. Currently, we do it the long way, saving it one PDF at a time using Save As. This works if it only a handful of letters but it becomes a pain when we're processing hundreds! Data source is an Excel file.

Any help would be greatly appreciated!
 
Hi Cindy, I'm a newby, I saw your code and it looks good. I wonder if there is a way to ,instead of a file dialog, save a mergefield as part of the field name?



Hi,
I have used the following macro to save mail merge output to individual files based on the value of a field in the datasource...maybe you can modify it to do what you need, or add in the logic into your macro that merges the data one at a time. It uses a different approach than the macros I found on the web. Instead of merging then separating, it loops through the datasource, merging and saving one record at a time.
The macro allows the user to select a directory for saving the files. It depends on an existing mail merge main document that already contains merge fields and a link to the datasource.
Hope this helps,

Code:
Sub merge1record_at_a_time()'
' merge1record_at_a_time Macro
'
'
    Dim fd As FileDialog


    'Create a FileDialog object as a Folder Picker dialog box.
    Set fd = Application.FileDialog(msoFileDialogFolderPicker)
    With fd


        'Use the Show method to display the Folder Picker dialog box and return the user's action.
        'The user pressed the button.
        If .Show = -1 Then
                For Each vrtSelectedItem In .SelectedItems


                'vrtSelectedItem is aString that contains the path of each selected item.
                'You can use any file I/O functions that you want to work with this path.
                'This example displays the path in a message box.
        SelectedPath = vrtSelectedItem


        Next vrtSelectedItem


        Else
        MsgBox ("No Directory Selected.  Exiting")
        Exit Sub
        End If
    End With


    'Set the object variable to Nothing.
    Set fd = Nothing


Application.ScreenUpdating = False


MainDoc = ActiveDocument.Name
    ChangeFileOpenDirectory SelectedPath
    For i = 1 To ActiveDocument.MailMerge.DataSource.RecordCount
        With ActiveDocument.MailMerge
            .Destination = wdSendToNewDocument
            .SuppressBlankLines = True
            With .DataSource
                .FirstRecord = i
                .LastRecord = i
                .ActiveRecord = i
                docname = .DataFields("ASP_Print").Value & ".docx"      ' ADDED CODE
            End With
            .Execute Pause:=False
    Application.ScreenUpdating = False
            
        End With
    ActiveDocument.SaveAs FileName:=docname, FileFormat:= _
        wdFormatXMLDocument, LockComments:=False, Password:="", AddToRecentFiles _
        :=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts _
        :=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
        SaveAsAOCELetter:=False
    ActiveWindow.Close


    Windows(MainDoc).Activate
    Next i
Application.ScreenUpdating = True


End Sub
 
Upvote 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi Kryptonian and Cindy.
Thanks for this thread - it's been really helpful. I'm stuck on the last hurdle. I used Kryptonian's new code so that the PDFs don't open but am left with a pile of merged word docs open. I've tried a few variations on this:


Code:
ActiveDocument.ExportAsFixedFormat OutputFileName:=docName, _
        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
ActiveWindow.Close
[/QUOTE]

But that doesn't work. Any tips welcome.
Hans
 
Upvote 0
Solved my own problem!

I made a typo when I posted my problem. I had meant to say that "ActiveDocument.Close" didn't work, but mistakenly typed "ActiveWindow.Close". I realized I hadn't tried that so put it in and hey presto!

Final code for anyone else:

Code:
ActiveDocument.ExportAsFixedFormat OutputFileName:=docName, _
        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
ActiveWindow.Close SaveChanges:=wdDoNotSaveChanges

Hans
 
Upvote 0
Just wanted to say "Thank you so much" for putting up such a useful thread!

Now how can i get it to:
1) merge to PDF and then attach to an email and send the email
2) instead of looping through each document, loop through each change of company name and print to PDF all records for that company name, then attach to email and send the email.

Thanks everyone!
 
Upvote 0
That is rather beyond the capabilities of anything discussed in this thread. I believe it can be done with the Many-to-One Mail Merge add-in by Doug Robbins, available at http://bit.ly/1hduSCB
 
Upvote 0
HELP!!! I am fairly new to VBA etc and im trying to copy the macro into with Cindy's added bit too. I don't seem to having any luck - are you able to provide an idiots step by step guide of when I should be creating/adding the macro - how I should be completing the merge etc.
 
Upvote 0
You add the macro to your mailmerge main document and, instead of the usual 'Finish' stage, run the macro. Alternatively, if you rename the macro to MailMergeToDoc, clicking on 'Finish & Merge>Edit individual Documents' will run it automatically.
 
Upvote 0
You add the macro to your mailmerge main document and, instead of the usual 'Finish' stage, run the macro. Alternatively, if you rename the macro to MailMergeToDoc, clicking on 'Finish & Merge>Edit individual Documents' will run it automatically.

When I run the macro I get the following message:

Complile error: Variable not defined and it highlights the first line Sub merge1record_at_a time ()
 
Upvote 0

Forum statistics

Threads
1,215,124
Messages
6,123,187
Members
449,090
Latest member
bes000

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