Compensation_Sensation
New Member
- Joined
- Mar 25, 2020
- Messages
- 3
- Office Version
- 365
- Platform
- Windows
Good day all,
I tried replying to another similar post, but didn't get a response so I'm trying again with a new thread.
Basically, I have an excel file that has a number of fields that I've used to create mail merge word, and in turn create separate docs. What I can't lock down is how to tell it to save the file in a folder based on cell value.
Columns A through J are all used for the actual mail merge, and H is a name that I want the record from the mail merge to save in. So, If Row 2 specifies "Ron", and Row 3 specifies "Steve", each PDF should save in the specified folder. If I need to define the names of each folder within VBA (If I can get it to create if not already created, that'd be great too).
Right now, I can get it to save PDFs and Word Docs in separate folders, but that's as far as I've gotten.
Code for the mail merge follows (borrowed from another source):
I tried replying to another similar post, but didn't get a response so I'm trying again with a new thread.
Basically, I have an excel file that has a number of fields that I've used to create mail merge word, and in turn create separate docs. What I can't lock down is how to tell it to save the file in a folder based on cell value.
Columns A through J are all used for the actual mail merge, and H is a name that I want the record from the mail merge to save in. So, If Row 2 specifies "Ron", and Row 3 specifies "Steve", each PDF should save in the specified folder. If I need to define the names of each folder within VBA (If I can get it to create if not already created, that'd be great too).
Right now, I can get it to save PDFs and Word Docs in separate folders, but that's as far as I've gotten.
Code for the mail merge follows (borrowed from another source):
VBA Code:
Option Explicit
Const PDF_SAVED As String = "C:\Leader Folders\2019 TR Statements\Ron - TEST ENVIRONMENT\PDFs\"
Const WORD_SAVED As String = "C:\Leader Folders\2019 TR Statements\Ron - TEST ENVIRONMENT\Word Docs\"
Const SOURCE_FILE_PATH As String = "C:\Leader Folders\2019 TR Statements\Ron - TEST ENVIRONMENT\Mail Merge Source Data Ron TEST.xlsx"
Sub TestRun()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim MainDoc As Document, TargetDoc As Document
Dim dbPath As String
Dim recordNumber As Long, totalRecord As Long
Set MainDoc = ActiveDocument
With MainDoc.MailMerge
.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 WORD_SAVED & .DataSource.DataFields("Employee_Name").Value & ".docx", wdFormatDocumentDefault
TargetDoc.ExportAsFixedFormat PDF_SAVED & .DataSource.DataFields("Employee_Name").Value & ".pdf", exportformat:=wdExportFormatPDF
TargetDoc.Close False
Set TargetDoc = Nothing
Next recordNumber
End With
Set MainDoc = Nothing
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub