0% found this document useful (0 votes)
64 views2 pages

Option Explicit

This macro extracts data from an Excel file, uses that data to mail merge and generate individual PDF documents for each record. It saves the PDFs to a specified folder. The macro opens the data source, loops through each record, performs the mail merge to create a new document, saves as a DOCX and PDF file using the record's name field, closes the document, and deletes the temporary DOCX files. It then cleans up and exits.

Uploaded by

hcga pcs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views2 pages

Option Explicit

This macro extracts data from an Excel file, uses that data to mail merge and generate individual PDF documents for each record. It saves the PDFs to a specified folder. The macro opens the data source, loops through each record, performs the mail merge to create a new document, saves as a DOCX and PDF file using the record's name field, closes the document, and deletes the temporary DOCX files. It then cleans up and exits.

Uploaded by

hcga pcs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Option Explicit

Const FOLDER_SAVED As String = "D:\SLIP KST IWIP\"

Const SOURCE_FILE_PATH As String = "D:\MUHSIN\06. JUNI 2022\RULLY\IWIP\Data Slip tid iwip


jun22.xlsx"

Sub MailMergeToIndPDF()

Dim MainDoc As Document, TargetDoc As Document

Dim dbPath As String

Dim recordNumber As Long, totalRecord As Long

Set MainDoc = ActiveDocument

With MainDoc.MailMerge

'// if you want to specify your data, insert a WHERE clause in the SQL statement

.OpenDataSource Name:=SOURCE_FILE_PATH, sqlstatement:="SELECT * FROM [Sheet1$]"

totalRecord = .DataSource.RecordCount

For recordNumber = 1 To totalRecord

With .DataSource

.ActiveRecord = recordNumber

.FirstRecord = recordNumber

.LastRecord = recordNumber

End With

.Destination = wdSendToNewDocument

.Execute False
Set TargetDoc = ActiveDocument

TargetDoc.SaveAs2 FOLDER_SAVED & .DataSource.DataFields("naama").Value & ".docx",


wdFormatDocumentDefault

TargetDoc.ExportAsFixedFormat FOLDER_SAVED & .DataSource.DataFields("naama").Value &


".pdf", exportformat:=wdExportFormatPDF

TargetDoc.Close False

Set TargetDoc = Nothing

Next recordNumber

End With

On Error Resume Next

Kill FOLDER_SAVED & "*.docx"

On Error GoTo 0

Set MainDoc = Nothing

End Sub

You might also like