Search code examples
vbams-wordms-access-2010mailmerge

Losing format with MS Word Mailmerge Macro


Using MS Word 2010 I want a Mailmerge to run with a macro, saving each record out as a separate file, in PDF format using one of the fields as the filename. This will save me loads of time.

The problem i've got is that the format is being TOTALLY lost, as though it's just copying text and pasting it in a new document. Is there any way I can protect the formatting as without it it's pretty fruitless...

Thanks in advance.

Sub splitter()

Dim i As Integer
Dim Source As Document
Dim Target As Document
Dim Letter As Range
Dim oField As Field
Dim FileNum As String

Set Source = ActiveDocument

ActiveDocument.MailMerge.DataSource.ActiveRecord = wdLastRecord

For i = 1 To ActiveDocument.MailMerge.DataSource.ActiveRecord
    ActiveDocument.MailMerge.DataSource.ActiveRecord = i
    Set Letter = Source.Range
        For Each oField In Letter.Fields
        If oField.Type = wdFieldMergeField Then
            If InStr(oField.Code.Text, "INV_ID") > 0 Then
            FileNum = oField.Result
            End If
        End If
        Next oField
    Set Target = Documents.Add
    Target.Range = Letter
    Target.SaveAs2 "C:\BACS\INVOICING\INVOICES\Word Export\" & FileNum, 17
    Target.Close
    Next i
End Sub

Solution

  • How about using Save?

    This sample code loops through each mailmerge item in a mailmerge document, opens the item as a letter and saves it to a PDF using a field in the DataSource as a file name. There is no error coding and no attempt to check for duplicate file names. It is a snippet.

    Dim iRec As Integer
    Dim docMail As Document
    Dim docLetters As Document
    
    
    Set docMail = ActiveDocument
    
    ''There is a problem with the recordcount property returning -1
    ''http://msdn.microsoft.com/en-us/library/office/ff838901.aspx
    docMail.MailMerge.DataSource.ActiveRecord = wdLastRecord
    iRec = docMail.MailMerge.DataSource.ActiveRecord
    
    docMail.MailMerge.DataSource.ActiveRecord = wdFirstRecord
    
    For i = 1 To iRec
        With docMail.MailMerge
            .Destination = wdSendToNewDocument
            .SuppressBlankLines = True
            With .DataSource
                .FirstRecord = i
                .LastRecord = i
                '' This will be the file name
                '' the test data source had unique surnames
                '' in a field (column) called Surname
                sFName = .DataFields("Surname").Value
            End With
            .Execute Pause:=False
            Set docLetters = ActiveDocument
        End With
        docLetters.ExportAsFixedFormat OutputFileName:= _
            "Z:\docs\" & sFName & ".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
        docLetters.Close False
    
        docMail.MailMerge.DataSource.ActiveRecord = wdNextRecord
    Next