This tutorial guides you through automating the Mail Merge process in Microsoft Word using VBA (Visual Basic for Applications). Mail Merge is a powerful feature in Word that allows you to create personalized documents for multiple recipients using a single template. By using VBA, you can further enhance this process to automatically save each merged document as both a DOCX and a PDF file, all while keeping your files organized in a specific directory.
Here’s what you will achieve by the end of this tutorial:
- Set up your Word and Excel files for Mail Merge.
- Write and customize a VBA script to automate the Mail Merge process.
- Save each merged document individually in both DOCX and PDF formats.
- Optionally clean up temporary files to keep your directory tidy.
Let’s get started!
Step-by-Step Tutorial for Mail Merge with VBA in Word
Step 1: Ensure Files are in the Same Directory
- Place the Master Document and Data Source File in the Same Directory:
- Ensure that both your Word document (the master document) and your Excel file (the data source) are saved in the same folder. For example, both files could be located in
A:\01-kuliah\2024-PELATIHAN-EDTECH\
.
- Ensure that both your Word document (the master document) and your Excel file (the data source) are saved in the same folder. For example, both files could be located in
Step 2: Open the Master Document in Word
- Open Microsoft Word:
- Open the Word document that will serve as the master document for your mail merge.
Step 3: Open the VBA Editor
- Open the VBA Editor:
- Press
ALT + F11
to open the Visual Basic for Applications (VBA) editor.
- Press
Step 4: Insert a New Module
- Insert a Module:
- In the VBA editor, go to
Insert
>Module
. This will create a new module where you can write and store your VBA code.
- In the VBA editor, go to
Step 5: Copy and Paste the VBA Code
- Copy the VBA Code:
- Copy the following VBA code and paste it into the newly created module in the VBA editor:
Step 6: Customize the VBA Code
- Customize the Folder and File Paths:
- Modify
FOLDER_SAVED
andSOURCE_FILE_PATH
constants to match your specific directory and file names.
- Modify
- Customize the SQL Statement:
- If needed, adjust the SQL statement in
.OpenDataSource
to filter or modify the data being merged.
- If needed, adjust the SQL statement in
- Customize the File Naming Convention:
- Change how the files are named in the
TargetDoc.SaveAs2
andTargetDoc.ExportAsFixedFormat
lines based on your specific needs.
- Change how the files are named in the
Step 7: Run the VBA Macro
- Run the Macro:
- In the VBA editor, press
F5
or go toRun
>Run Sub/UserForm
to execute the macro.
- In the VBA editor, press
- Check the Output:
- After running the macro, check the specified folder (e.g.,
A:\
) to find the generated DOCX and PDF files.
- After running the macro, check the specified folder (e.g.,
- Optional Clean-Up:
- If you do not want to delete the DOCX files after creating the PDFs, you can comment out or remove the following lines in the code:
On Error Resume Next
Kill FOLDER_SAVED & "*.docx"
On Error GoTo 0
Notes
- Ensure that macros are enabled in Word. You might need to adjust your security settings to allow macros to run.
- Make sure that the Excel file is not open in another program when running the macro.
- This tutorial assumes that the data source Excel file has a sheet named
sertifikat
. Adjust the sheet name in the SQL statement if your data is in a different sheet.
By following these steps, you can automate the mail merge process in Word using VBA, saving each record individually as DOCX and PDF files.
Environments
- Windows 11 Pro
- Microsoft Excel 365 Enterprise
- Microsoft® Word for Microsoft 365 MSO (Version 2406 Build 16.0.17726.20078) 64-bit