Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 01-08-2023, 08:33 PM
ahdiethya ahdiethya is offline How to Save File with 2 different criteria - mail merge - individual documents vba Windows 10 How to Save File with 2 different criteria - mail merge - individual documents vba Office 2019
Novice
How to Save File with 2 different criteria - mail merge - individual documents vba
 
Join Date: Jan 2023
Posts: 2
ahdiethya is on a distinguished road
Post How to Save File with 2 different criteria - mail merge - individual documents vba

Hii,
I am trying to automate mail merge to save each record individually in PDF output using VBA, but when i try to save file with 2 different criteria (FSName = .DataFields("Nomor Polis").Value & " - " & .DataFields("Nama Tertanggung").Value) it won't work.


Option Explicit
Const FOLDER_SAVED As String = "C:\Users\adyudh\Desktop\Testing eSertifikat\BPR Group\Output"
Const SOURCE_FILE_PATH As String = "C:\Users\adyudh\Desktop\Testing eSertifikat\BPR Group\Testing - Copy.xlsx"

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

Set MainDoc = ActiveDocument
With MainDoc.MailMerge

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

totalRecord = .DataSource.RecordCount

For recordNumber = 1 To totalRecord

With .DataSource
.ActiveRecord = recordNumber
.FirstRecord = recordNumber
.LastRecord = recordNumber

FSName = .DataSource.DataFields("Nomor Polis").Value & " - " & .DataSource.DataFields("Nama Tertanggung").Value



End With

.Destination = wdSendToNewDocument
.Execute False

Set TargetDoc = ActiveDocument

TargetDoc.SaveAs FOLDER_SAVED & FSName & ".docx", wdFormatDocumentDefault
TargetDoc.ExportAsFixedFormat FOLDER_SAVED & FSName & ".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
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Mail merge and saving individual documents Freya9714 Mail Merge 1 07-05-2020 01:10 AM
How to Save File with 2 different criteria - mail merge - individual documents vba Save each individual merged document as its own file sdemuth@earthlink.net Mail Merge 7 10-07-2019 11:21 PM
How to Save File with 2 different criteria - mail merge - individual documents vba Compatibility of 2 macros in mail merge: Delete table rows + save individual PDFs Btop Word VBA 26 03-07-2018 01:45 PM
How to Save File with 2 different criteria - mail merge - individual documents vba Mail Merge To Individual PDF Files iamrickdeans Mail Merge 1 01-15-2014 12:46 AM
How to Save File with 2 different criteria - mail merge - individual documents vba Word Mail Merge File Save mickeyw3340 Mail Merge 2 12-18-2012 11:30 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:12 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft