How to Generate PDF from Mail Merge | Mail Merge Save Separate Files | Mail Merge to PDF and Doc

Поділитися
Вставка
  • Опубліковано 11 гру 2024

КОМЕНТАРІ • 10

  • @JeffFahndrich
    @JeffFahndrich 2 роки тому +4

    THIS WORKS! Thank you so so much! I will add that if the file name you want to create is not a number, use "As String" instead of "As Long" in the first line otherwise you will get a "Runtime Error 13".
    Also if it helps, below is the VBA code so you can copy-paste and not be like me literally re-typing everything he has on the screen. Just pay attention to anything in {{ }}:
    Sub GetPDF()
    Dim {{merge field}} As String, file_path, folder_path As String
    folder_path = "C:\Users\{{Username}}\{{other folders}}"
    If Dir(folder_path, vbDirectory) = "" Then
    MkDir (folder_path)
    End If
    Dim masterDoc As Document, singleDoc As Document, lastRecordNum As Long
    Set masterDoc = ActiveDocument
    masterDoc.MailMerge.DataSource.ActiveRecord = wdLastRecord
    lastRecordNum = masterDoc.MailMerge.DataSource.ActiveRecord
    masterDoc.MailMerge.DataSource.ActiveRecord = wdFirstRecord
    Do While lastRecordNum > 0
    masterDoc.MailMerge.Destination = wdSendToNewDocument
    masterDoc.MailMerge.DataSource.FirstRecord = masterDoc.MailMerge.DataSource.ActiveRecord
    masterDoc.MailMerge.DataSource.LastRecord = masterDoc.MailMerge.DataSource.ActiveRecord
    masterDoc.MailMerge.Execute False
    {{merge field}} = masterDoc.MailMerge.DataSource.DataFields("{{merge field}}").Value
    Set singleDoc = ActiveDocument
    singleDoc.SaveAs2 FileName:=folder_path & "\" & {{merge field}} & ".docx", FileFormat:=wdFormatXMLDocument
    singleDoc.ExportAsFixedFormat OutputFileName:=folder_path & "\" & {{merge field}} & ".pdf", ExportFormat:=wdExportFormatPDF
    singleDoc.Close False
    If masterDoc.MailMerge.DataSource.ActiveRecord >= lastRecordNum Then
    lastRecordNum = 0
    Else
    masterDoc.MailMerge.DataSource.ActiveRecord = wdNextRecord
    End If
    Loop
    End Sub

    • @Python2020
      @Python2020  2 роки тому

      Thank you it might help others

  • @cpkanojiya
    @cpkanojiya Рік тому +1

    Can you make this video in python laguage.

    • @Python2020
      @Python2020  Рік тому

      It is already there sir, just brouse my channel video no 12

  • @supatkhongfak3661
    @supatkhongfak3661 Рік тому +1

    how can I get that code ?
    thank you

  • @kaira7793
    @kaira7793 6 місяців тому

    Tried this but it is giving me 5487 cannot complete due to permission

    • @Python2020
      @Python2020  6 місяців тому +1

      Not sure if any restriction. You may check options an security..

  • @litolapis9691
    @litolapis9691 10 місяців тому +1

    Hi can i have the code. Thank you

    • @Python2020
      @Python2020  10 місяців тому

      It is same as in the video.. thank you :)