Thread: [Solved] Using VBA for Mail Merging
View Single Post
 
Old 05-30-2019, 09:15 AM
vbaformailmerge vbaformailmerge is offline Windows 10 Office 2016
Novice
 
Join Date: May 2019
Posts: 4
vbaformailmerge is on a distinguished road
Unhappy Using VBA for Mail Merging

I have been playing around with codes but I cannot figure out myself how to do this properly.

I have large databases and letter templates in which I normally would use the Finish & Merge button to merge these documents into a pdf within increments - For example;

If my database contained 3000 records I would split the records to be saved from records 1-1000 until I reach the end. I would greatly appreciate if anybody could help me with what to do? I would need a code to cycle through a loop and save the records as pdfs but in increments of a variable.

this was the code when I recorded a macro:
Code:
Sub test2()
'
' test2 Macro
'

    With ActiveDocument.MailMerge
        .Destination = wdSendToNewDocument
        .SuppressBlankLines = True
        With .DataSource
            .FirstRecord = 1
            .lastRecord = .FirstRecord + 15
        End With
        .Execute Pause:=False
    End With
    ActiveDocument.ExportAsFixedFormat OutputFileName:= _
        "1-15 test.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
    ChangeFileOpenDirectory _
        "filedirectory"
    Windows("filename.doc").Activate
    With ActiveDocument.MailMerge
        .Destination = wdSendToNewDocument
        .SuppressBlankLines = True
        With .DataSource
            .FirstRecord = 15
            .lastRecord = .FirstRecord + 15
        End With
        .Execute Pause:=False
    End With
    ActiveDocument.ExportAsFixedFormat OutputFileName:= _
        "15-29 records.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
    ChangeFileOpenDirectory _
        "file directory"
    Windows("file.doc").Activate
End Sub
Option Explicit
(I have changed the filename locations to random ones to hide protected data)

I have been using it with other code I found on this forum so I am unsure how to put this into a loop?

If anyone knows of a way to do this or any tips/code I could work off of, I would be eternally grateful

Last edited by vbaformailmerge; 05-31-2019 at 02:18 AM.
Reply With Quote