Free Support Forum - aspose.com

Change datasource in document before MailMerge.Execute()

Dear mr./ms./mrs.,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

We have a question regarding the use of a prepared MailMerge MS-Word document. We have attached the zipped MS-Word document and a datafile (Wfimdeb.ccc).

In case you want to use it please extract it in C:\temp so the datafile can be found by MS-Word.

While creating we selected * from a rtf text file named Wfimdeb.ccc (see attached document), put some MailMerge fields in the document and we have saved the document.

When you open the MS-Word document in MS-Word you are able to preview the data, because the datafile (.ccc) has been used while creating the MS-Word document and the data in that file will be used when the mailmerge is started (in MS-Word).

However when we open the document with the Aspose.Words.Document class we are not able to change the mailmerge data before mailmerging it. Only during the mailmerging it can be changed but then the document is mailmerged and cannot be edited anymore (like before the MailMerge was started)

So instead of executing the actual mailmerge (with Document.MailMerge.Execute() ) we wish to already put the data in the document and then save it again.

This would be something like: Document.MailMerge.DataSource = MyDataRow

Then we save the MS-Word document and when we open it will contain the data from the DataSource.

Please inform me if this is possible or how to achieve this.

Kind regards,

Jan Stolk.

CarWise Information Services
Almere, The Netherlands.

Hello Jan,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. I think you can edit your data before mail merging it and execute mail merge directly before saving document. For example see the following code:

//Open template

Document doc = new Document(@"Test179\in.doc");

//Edit data before mailmerge

row["Naam"] = "Jan";

//You can also change values of other fields

//..................................

//Execute mail merge

doc.MailMerge.Execute(row);

//Save document

doc.Save(@"Test179\out.doc");

Best regards.

Hi,

That part is clear to me.

However, i do not want to do the actual mailmerge yet. I only want to change the record/data and save the document again. In order for the user to be able to open the document with the proper record/data and still be able to add an extra mailmerge field to the document before the mailmerge is done. When the mailmerge is executed it is not possible anymore to select other mailmerge fields from the record/data.

Regards,

Jan.

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for additional information. Unfortunately you can’t specify Data Source for document. You can change your template before processing document using Aspose.Words.

Best regards.

Hi Alexy,

Thank you for your support.

Regards,

Jan.