How to Handle Empty CSV Data Sources in Word Mail Merge
Question details
The user needs to prevent a mail merge workflow from outputting raw field names when the primary CSV data source contains no records, opting instead to display a default message.

- Product
- Microsoft Word (via PowerShell automation)
- Device & OS
- not provided
- Scenario
- Running an automated PowerShell and Word mail-merge workflow where the primary CSV file exported from SQL occasionally has no data.
- Observed behavior
- When the CSV has no records, the workflow creates a PDF that visibly displays the raw mail merge field names instead of rendering blank fields or a default fallback message.
Ensure you have access to edit your PowerShell mail-merge script and have prepared a backup CSV file containing your desired default text (e.g., 'No Records Today').
Validate CSV File in PowerShell Before Executing Mail Merge
Use PowerShell to determine if the CSV data source contains data. If it is empty, redirect the mail merge to use a backup CSV or an alternative template before opening Word.
Handling the empty CSV issue at the scripting level is the most reliable approach. By intercepting the process before Microsoft Word even opens, you prevent the mail merge macro from blindly processing an empty file and generating a PDF filled with raw merge tags.
Locate and open the PowerShell script responsible for launching your mail merge workflow using a script editor like PowerShell ISE or Visual Studio Code.
Insert a command to check if the primary SQL export CSV has data. You can do this by using the 'Import-Csv' cmdlet and checking the row count to see if it contains a first record.
Write an 'If/Else' statement based on the row count. If the primary CSV is empty, instruct the script to assign a backup CSV (containing default fallback values) as the data source variable.
Allow the script to launch the Word application and execute the mail merge macro using the conditionally assigned data source file.

Perform Seamless Mail Merges with WPS Office Writer
If you are looking for a reliable, lightweight suite for your document automation and mail merge tasks, WPS Office is an excellent free alternative to Microsoft Office. It offers robust compatibility with standard data sources and a highly intuitive interface.
- 1. Open WPS Writer: Launch WPS Office and open a new or existing document to serve as your mail merge template.
- 2. Access Mail Merge: Navigate to the References tab and click on Mail Merge to activate the mail merge toolbar.
- 3. Import Data Source: Click Open Data Source and select your prepared CSV or Excel spreadsheet containing your records.

Frequently Asked Questions
Why does my mail merge show field names instead of data?
When a mail merge data source like a CSV file has no records, the merge process cannot match or populate the fields. As a result, the word processor defaults to displaying the raw merge field tags (e.g., <<FirstName>>) in the final output document.
Can I set default text inside Word for an empty mail merge field?
While you can use rules like 'If...Then...Else' inside Word to check for blank individual fields within an existing record, an entirely empty CSV file without any data rows will not trigger these rules properly. Using a backup data source via script is a much more robust solution.
How do I manually select a different data source during a mail merge?
Under the Mailings or References tab, click on 'Select Recipients' (or 'Open Data Source') and choose 'Use an Existing List' to browse your computer and select a different CSV or spreadsheet file containing the correct data.




