Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Other Office > Word VBA
Word VBA Discuss using VBA to program Word.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Word VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old February 14th, 2013, 06:09 PM
Registered User
Join Date: Feb 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Loop VBA with Mail Merge


I am working on a project in which a mail merge document will ultimately generate several PDF documents (1 PDF per Merge Record). The PDF document itself is saved with a file name based on a merge field on the master merge document. The problem I am having is finding a way to loop through to the next merge record and repeat until all merge records have generated their own PDF.

I am using the code below as a starting point:
Sub Macro1()
' Macro1 Macro

   With ActiveDocument.MailMerge
        .Destination = wdSendToNewDocument
        .SuppressBlankLines = True
        With .DataSource
            .FirstRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
            .LastRecord = ActiveDocument.MailMerge.DataSource.LastRecord
        End With
        .Execute Pause:=False
    End With
    ActiveWindow.ActivePane.VerticalPercentScrolled = 64
        ActiveDocument.ExportAsFixedFormat OutputFileName:= _
        "Q:\FILE PATH\" & _
        Documents("MasterTemplate.doc").MailMerge.DataSource.DataFields("File_Name").Value & ".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
End Sub
I am working in Word 2007 and I know there are ways to merge to individual documents built in, however, I need to use the code as there is an image that is generated based on a merge field. This requires that the individual record be updated using Selection.WholeStory and Selection.Fields.Update

If anyone knows how to get this code to repeat (it works PERFECT for the first record) for each record that would be swell!


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Mail Merge adding unwanted pages jeffalex2 Word VBA 0 July 27th, 2011 08:04 AM
Help with mail merge! N3on Excel VBA 1 July 25th, 2011 02:40 PM
Mail Merge VBA Query jayp Excel VBA 1 October 14th, 2005 05:25 PM
Need Help In Mail Merge raghur Access VBA 0 September 8th, 2005 05:46 AM
Access/Vba mail merge to word help alfonse Access VBA 0 July 28th, 2005 05:11 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.