We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Execute Mail Merge on Word Template Document with Saved ODC Data Source C# .NET

Hi.

I’m presently evaluating Aspose.words for .NET for use in a new project. So far, I’m very happy with it, but I seem to have hit a snag. There will be times when I want to execute a MailMerge without passing in a data source, because the document already has a saved data source. However, there is no parameterless Execute() method on the MailMerge class. It seems that you always have to pass the data source in.

Can you help me out with an example, or is it impossible to do what I’m asking? I’ve seen topics in this forum which explain how to set the datasource and save it, but nothing about how to simply use the saved datasource in a MailMerge operation.

Many thanks

@LukeWebber,

To perform a mail merge operation on Word document, you need to provide at least one parameter to the MailMerge.Execute method (see different overloads of MailMerge.Execute method). To pass an empty data source, you may use following code:

Document doc = new Document("C:\\Temp\\mergefields.docx");

MailMerge mailMerge = doc.MailMerge;
mailMerge.Execute(new DataTable());
// mailMerge.Execute(new string[] { "" }, new object[] { "" });

doc.Save("C:\\Temp\\word.docx");

But if I pass an empty DataTable, won’t the merge do nothing at all? What I want is to perform a merge using the data source that is stored in the document.
To confirm, I’ve just tried what you suggest, and it created and output file with the merge fields unfilled…
image.png (4.5 KB)

@LukeWebber,

What do you mean by “data source that is stored in the document”? Unfortunately, your question isn’t clear enough therefore we request you to please elaborate your inquiry further by providing complete details of your use case. Also, please compress the following resources into ZIP format and attach the .zip file here for testing:

  • A simplified source template Word document containing the merge fields and data source
  • Aspose.Words generated output DOCX file showing the undesired behavior
  • Your expected Word file showing the desired output. You can create this file manually by using MS Word. It would be great if you please also list the complete steps that you performed in MS Word to create the expected file.

This will help us to understand your scenario, and we will be in a better position to address your concerns accordingly.

Hi Awais.

If you look at the attached document, it has a data source of type Native, pointing to a data source at “C:\Users\Luke\Documents\My Data Sources\Membership_Person.odc”. How would I go about merging that? I know the API is aware of that data source,m because all of the details are showing in the MailMergeSettings property. Do I have to interpret all of those settings and open the data source myself to feed it back in for the MailMerge.Execute?

FormLetterTest.docx (12.9 KB)

@LukeWebber,

Yes, your understanding is correct. You need to parse the data source at location specified by MailMergeSettings.DataSource by using the standard .NET methods/classes and populate DataTable object for example and then pass DataTable object to MailMerge.Execute method to programmatically perform mail merge.