Question

I am currently trying to use mailmerge to generate custom reports for customers. I would like to be able to save the individual pages that are the result of the mailmerge using the specific customercode that is present on every page.

The location of the customer code is located on exactly the same location on every page. Saving individual pdfs using simple VBA code that scrolls trough the pages is already working.

Could somebody give me a hint on where to look or what to do to get his working?

Sub pdf()
'Splits up document per page
Dim numPages As Integer
Dim runner As Integer
Dim file As String
numPages = ActiveDocument.ComputeStatistics(wdStatisticPages)
runner = 1

While runner <= numPages
    file = CStr(runner) + ".pdf"
    ActiveDocument.ExportAsFixedFormat OutputFileName:=file _
        , ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
        wdExportOptimizeForPrint, Range:=wdExportFromTo, From:=runner, To:=runner, Item:= _
        wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
        CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
        BitmapMissingFonts:=True, UseISO19005_1:=True
    ChangeFileOpenDirectory "C:\output\"
    runner = runner + 1

Wend
End Sub
Was it helpful?

Solution

I was on the wrong path - instead of actually generating the final mailmerge I found a way to loop trough all 'documents'. While doing so the original fields stay available for manipulation withing VBA. Hopefully somebody else can use my trick to circumvent the not so productive mailmerge.

Find attached the solution to my problem:

Sub pdf()
'Splits up document per page
Dim numPages As Integer
Dim runner As Integer
Dim file As String
numPages = ActiveDocument.ComputeStatistics(wdStatisticPages)
runner = 1

Dim fileNameRunner
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdFirstRecord
While runner <= numPages - 1

    file = ActiveDocument.MailMerge.DataSource.DataFields("CODE").Value

    ActiveDocument.ExportAsFixedFormat OutputFileName:=file _
        , ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
        wdExportOptimizeForPrint, Range:=wdExportFromTo, From:=1, To:=1, Item:= _
        wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
        CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
        BitmapMissingFonts:=True, UseISO19005_1:=True
    ChangeFileOpenDirectory "C:\output\"
    runner = runner + 1
    ActiveDocument.MailMerge.DataSource.ActiveRecord = wdNextRecord

Wend
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top