Hi @Uptergrove, Carolyn,
Thank you for posting your question in the Microsoft Q&A forum and for sharing the details and screenshot, that context really helps.
The Run-time error 5852 (“Requested object is not available”) in a Mail Merge macro typically occurs when Word tries to reference a Mail Merge object (such as the DataSource, ActiveRecord, or a specific DataField) before the data source is fully connected, or when the expected record/field is not available at that moment.
Based on the highlighted line in your code:
masterDoc.MailMerge.DataSource.ActiveRecord = wdLastRecord
this error most commonly happens in a few situations:
1/ The mail merge hasn’t been fully initialized:
For example, if the data source wasn’t successfully linked (or the document isn’t truly in a Mail Merge “main document” state), Word won’t be able to move to the last record.
2/ The data source is connected, but there are no available records:
This can happen if the source file path is incorrect, the source opens without data, or Word can’t read the list as expected.
3/ One or more fields referenced in the macro don’t exist in the data source:
Since your macro calls fields such as:
DataFields("PdfFolderPath")
DataFields("PdfFileName")
Word will throw this type of error if a field name is missing, spelled differently, or not being recognized from the source.
4/ The wrong document is active or the document is restricted:
In some cases, the macro is run from an output document (instead of the original merge template), or the document is protected, which can prevent Mail Merge objects from behaving normally.
Here are a few checks to help isolate the issue:
1/ Confirm Word recognizes this file as the Mail Merge main document:
In Word, go to Mailings > Start Mail Merge, and make sure it’s set correctly (for example, not reset to a normal document). Then reconnect the data source and test again.
2/ Validate the data source headers (field/column names):
Please double‑check your spreadsheet/source file includes the exact field names used in the macro (including spelling and spacing), such as:
- Cost-it No
- PdfFolderPath
- PdfFileName (and any other fields your script depends on).
3/ Add a safeguard before jumping to the last record:
This prevents Word from trying to move to a record when none exist:
If masterDoc.MailMerge.DataSource.RecordCount > 0 Then
masterDoc.MailMerge.DataSource.ActiveRecord = wdLastRecord
End If
4/ Make sure the merge is fully ready before referencing the output document:
Sometimes Word needs a moment to finish creating the merged output, and referencing ActiveDocument too quickly can trigger object‑availability errors.
You can also review the same issue with your case here: How to Fix Run-Time Error "5852".
I hope this information helps move things forward. Please feel free to let me know how it goes, or if you need any further assistance. I’ll be happy to help.
I look forward to your reply.
If the answer is helpful, click on "Accept Answer" and please vote positively. If you have additional questions about this answer, click on "Comment".
Note: Follow the steps in our documentation to enable email notifications if you want to receive the email notification related to this thread.