0% found this document useful (0 votes)
28 views

Scripts VBA Mail Marge ke PDF email

This VBA script automates the mail merge process in Microsoft Word, using data from an Excel file to generate individual documents and PDFs for each record. It saves the generated files in a specified folder and sends emails with the PDFs attached to the respective recipients. The script also includes a cleanup step to delete the temporary Word documents after the emails are sent.

Uploaded by

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

Scripts VBA Mail Marge ke PDF email

This VBA script automates the mail merge process in Microsoft Word, using data from an Excel file to generate individual documents and PDFs for each record. It saves the generated files in a specified folder and sends emails with the PDFs attached to the respective recipients. The script also includes a cleanup step to delete the temporary Word documents after the emails are sent.

Uploaded by

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

Option Explicit

Const FOLDER_SAVED As String = "D:\TUTORIAL\PART2\Surat Tugas-" 'sesuaikan


direktorinya
Const SOURCE_FILE_PATH As String = "D:\TUTORIAL\PART2\database.xlsx" 'sesuaikan
direktorinya

Sub MailMergeToIndPDF()
Dim MainDoc As Document, TargetDoc As Document
Dim dbPath As String
Dim recordNumber As Long, totalRecord As Long

Dim objOutlook As Object


Dim objMail As Object
Dim emailTo As String
Dim emailAttach As String
Dim emailDear As String

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("Nama").Value &


".docx", wdFormatDocumentDefault 'sesuaikan dengan field yang akan dijadikan format
penamaan
TargetDoc.ExportAsFixedFormat FOLDER_SAVED
& .DataSource.DataFields("Nama").Value & ".pdf", exportformat:=wdExportFormatPDF
'sesuaikan dengan field yang akan dijadikan format penamaan
TargetDoc.Close False

'//Scripts send email

Set objOutlook = CreateObject("Outlook.Application")


Set objMail = objOutlook.CreateItem(0)
emailTo = .DataSource.DataFields("Email").Value
emailAttach = FOLDER_SAVED & .DataSource.DataFields("Nama").Value &
".pdf"
emailDear = .DataSource.DataFields("Nama").Value

With objMail
.to = emailTo
.Subject = "Surat Tugas Dinas"
.htmlbody = "Dear " & emailDear & "<br><br>Dengan ini disampaikan Surat
Tugas Anda.<br>Mohon Cek Lampiran email ini<br><br><br> Best Regards,<br><br>Drs.
Supraptono<br><br>"
.Attachments.Add emailAttach
.Send 'Instead of .Display, you can use .Send to send the email _
or .Save to save a copy in the drafts folder
End With

Set objOutlook = Nothing


Set objMail = Nothing

Set TargetDoc = Nothing


Next recordNumber
End With
On Error Resume Next
Kill FOLDER_SAVED & "*.docx"
On Error GoTo 0
Set MainDoc = Nothing
End Sub
'adopted from https://learndataanalysis.org/automate-mail-merge-to-save-each-
record-individually-with-word-vba/
'with additional delete docx file

You might also like