VBA - How to split a word mail merge into multiple .pdf files

3

Personally, my case is this: I participate in the organization of some events in the area of IT and free software and the printing of certificates is always a problem. In the last event that I participated as an organizer the mission was to issue the certificates digitally, generating a PDF file for each certificate. How can I split a mail merge into different files, with custom names and in PDF?

    
asked by anonymous 21.07.2016 / 05:24

3 answers

3

So the solution I found was using VBA for application. The instructions:

1- Create mail merge in Word with participants name and other information as needed.

2 - Merge the mail merge so that the end result is a Word file with all the certificates.

3- In the file with all the certificates, write the following VBA code:

Sub BreakOnSection()
Dim Arquivo As Integer
Dim CaminhoArquivo As String
Dim TextoProximaLinha As String

'Set reading the file that contains the names of files that will be generated.
Arquivo = FreeFile
CaminhoArquivo = "F:\Documentos\Evento\participantes.txt"

'Open file for reading.
Open CaminhoArquivo For Input As Arquivo

'Used to set criteria for moving through the document by section.
Application.Browser.Target = wdBrowseSection

'A mail merge document ends with a section break next page.
'Subtracting one from the section count stop error message.
For i = 1 To ((ActiveDocument.Sections.Count) - 1)   
    'Note: If a document does not end with a section break,
    'substitute the following line of code for the one above:
    'For I = 1 To ActiveDocument.Sections.Count

    'Select and copy the section text to the clipboard.
    ActiveDocument.Bookmarks("\Section").Range.Copy

    'Create a new document to paste text from clipboard.
    Documents.Add
    Selection.Paste

    'Altera a orientação da página para paisagem
    Orientation
    'Deletes the last page (use only if necessary)
    DeleteLastLine

    'Removes the break that is copied at the end of the section, if any.
    Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
    Selection.Delete Unit:=wdCharacter, Count:=1
    ChangeFileOpenDirectory "F:\Documentos\Evento\Certificados\"

    'It makes the line reading
    Line Input #Arquivo, TextoProximaLinha
    TextoProximaLinha = TextoProximaLinha

    'Export to .pdf and customize the file name to the line that was read
     ActiveDocument.ExportAsFixedFormat OutputFileName:= _
    "F:\Documentos\Evento\Certificados\" & TextoProximaLinha & ".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

    'Closes the "temporary" file from Word without saving changes
    ActiveDocument.Close savechanges:=wdDoNotSaveChanges
    'Move the selection to the next section in the document.
    Application.Browser.Next
Next i
        ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Sub

Sub Orientation()
    'If the page orientation is portrait in it is changed to landscape
    'This is a particular case in issuing certificates. Make sure that in your case there is a need
    If Selection.PageSetup.Orientation = wdOrientPortrait Then
        Selection.PageSetup.Orientation = wdOrientLandscape
    Else
    Selection.PageSetup.Orientation = wdOrientPortrait
    End If
ActiveWindow.ActivePane.VerticalPercentScrolled = 0
End Sub

Sub DeleteLastLine()
'This is a particular case in issuing certificates. Make sure that in your case there is a need
    Selection.HomeKey Unit:=wdStory
    Selection.EndKey Unit:=wdStory
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.Delete Unit:=wdCharacter, Count:=1
End Sub

4- Run and be happy! = D

    
21.07.2016 / 15:15
0

You do not need all this code for me.

Just create this code below in the master document and execute it.

Reminder : The first field of the mail merge should be the student's name or should change to the correct number of your database in the code ( Name = ActiveDocument.MailMerge.DataSource. DataFields.Item ( 1 ). ) to the correct name.

Code

Sub GeraCertificadoPDF()
'
' Macro GeraCertificadoPDF
' Gera arquivos em pdf a partir de mala direta via VBA
'
    ActiveDocument.MailMerge.DataSource.ActiveRecord = wdFirstRecord
    For i = 1 To ActiveDocument.MailMerge.DataSource.RecordCount

        Nome = ActiveDocument.MailMerge.DataSource.DataFields.Item(1).Value

        ActiveDocument.ExportAsFixedFormat OutputFileName:= _
        "c:\temp\Certificados\" & Nome & ".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

        ActiveDocument.MailMerge.DataSource.ActiveRecord = wdNextRecord

    Next i
End Sub
    
04.10.2018 / 19:53
-3
Sub Macro1()

 Macro1 Macro
 Cria PDF de mala direta do word 2016

Set Mail Merge in the first register
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdFirstRecord
Mail Merge Count
qtde = 94
qtde = ActiveDocument.MailMerge.DataSource.RecordCount
arquivo = ActiveDocument.MailMerge.DataSource.FieldNames(1).Name
nome = "Nome do seu arquivo" & ActiveDocument.MailMerge.DataSource.DataFields("nickname").Value
Do While qtde > 0
    ActiveDocument.ExportAsFixedFormat OutputFileName:= _
    "D:\dir1\subdir1\subdir2\subdir3\" & nome & ".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
    ActiveDocument.MailMerge.DataSource.ActiveRecord = wdNextRecord
nome = "Nome do seu arquivo" & ActiveDocument.MailMerge.DataSource.DataFields("nickname").Value
qtde = qtde - 1
Loop
End Sub
    
22.01.2018 / 16:29