VBA to create individual PDFs from MailMerge, and save in specific folder based on cell value

Joined
Mar 25, 2020
Messages
3
Office Version
  1. 365
Platform
  1. 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):
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
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Things to note: the VBA code sits in the word doc I'm using to create the mail merge. If it's possible to have the code sit entirely in Excel, that'd be ideal.
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top