Merge Fields not working

Hello Syncfusion,

I'm not sure if I'm doing this all wrong or not but figured I'd ask.  I have a word document that I have put a bunch of merge fields in, this is to print a sheet of labels.  I run the code below and it just spits out the word document just showing the merge fields but not actually inserting the data from my list.


FileStream fileStreamPath = new FileStream($"{ webHostEnv.WebRootPath }\\Avery5160DevLabels.doc", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

 WordDocument wordDocument = new WordDocument(fileStreamPath, FormatType.Docx);

            //Creates an instance of “MailMergeDataTable” by specifying mail merge group name and “IEnumerable” collection

            MailMergeDataTable mailingData = new MailMergeDataTable("Members", membersList);

            //Performs Mail merge

            wordDocument.MailMerge.ExecuteGroup(mailingData);


            //Download Word document in the browser

            using (MemoryStream stream = new MemoryStream())

            {

                //Saves the Word document to MemoryStream

                wordDocument.Save(stream, FormatType.Docx);

                wordDocument.Close();

                stream.Position = 0;

                //Download Word document in the browser

                return stream;

            }


I've looked at the guide but can't seem to get it to work as described.


Thanks,

Andrew


5 Replies

LB Lokesh Baskar Syncfusion Team March 25, 2022 03:04 PM UTC

Hi Andrew,

We have tried to reproduce the reported problem using the simple Word document but, it is working properly at our end.

Please refer to the below UG documentation to execute group mail merge.
https://help.syncfusion.com/file-formats/docio/mail-merge/mail-merge-for-group#mail-merge-with-net-objects

We suspect that the reported issue might be due to file level information of  the Word document. So, could you please share us the below things at your end:
 1. Screenshot of the issue/Output Word document which generated at your end
 2. Syncfusion product version
 3. Input Word documents which used at your end.
This will be more helpful to reproduce the same issue at our end. Thereby, we will proceed further on the reported issue and provide you the appropriate solution at the earliest.

Note: If you have any confidential data in your document, please replace with some dummy data and provide us the same. We just need your document to recreate the problem you face.
 
Regards, 
Lokesh B 



AN Andrew March 31, 2022 02:22 AM UTC

Thanks for the reply, I was finishing up another part of my program and took a look at this tonight.  I had this linked to a table in my database but also unlinked it but neither worked.  I am running Syncfusion version 19.4.0.56. This is what it looks like when I run the merge and download the document:


Attachment: Avery5160DevLabels_ade269a2.zip


LB Lokesh Baskar Syncfusion Team March 31, 2022 01:02 PM UTC

Hi Andrew,

On further analyzing the given input Word document, TableStart and TableEnd (region of group) are not specified in correct format. So, fields are not updated properly. We have modified the input document and prepared the sample application as per your requirement.

In the input Word document, we have done the following changes:
  Inserted BeginGroup or TableStart before the table.
  Inserted EndGroup or TableEnd end of the table.

Please refer to the below link.
https://help.syncfusion.com/file-formats/docio/mail-merge/mail-merge-for-group#create-template-for-group-mail-merge

Please find the sample and input Word document from the below attachment:


In the above sample, we have done the following things:
 Open the word document
 Gets the member details
 Perform the mail merge
 Saves the word document

Please find the below links to know more about mail merge
https://help.syncfusion.com/file-formats/docio/working-with-mail-merge
https://help.syncfusion.com/file-formats/docio/mail-merge/mail-merge-for-group

https://help.syncfusion.com/file-formats/docio/mail-merge/mail-merge-events
https://github.com/SyncfusionExamples/Mail-Merge-Examples

Regards,

Lokesh B


Attachment: GroupMailMerge_(2)_1768f4bc.zip


AN Andrew March 31, 2022 02:44 PM UTC

Thanks for getting back to me.  So it works if I use your template but when I change my tableName fields to the Begin/End Group like yours has, it doesn't work.  I'm downloading a template from Avery and then linking it to my database, putting the merge fields in and then setting it back to normal word doc.  Not sure if this is the reason or not but what process did you use to make your template?


Thanks



LB Lokesh Baskar Syncfusion Team April 1, 2022 03:23 PM UTC

Hi Andrew,


We have modified your given input Word document at our end. In the modified input Word document, we have done the following things.

1.       We have removed the shapes from the Word document.

2.       We have removed TableEnd from the first cell and added that TableEnd in the last cell.

Please find the input Word document in the below attachment.

Could you please try this Word document at your end and provide us confirmation of whether the problem has been resolved or not.

If you still facing issue means, we suspect that the reported issue might be due to field values given to the Word document during the mail merge process. So, could you please share with us the modified sample (Field values and code snippet) to reproduce the same issue which you mentioned.

This will be more helpful to reproduce the same issue on our end. Thereby, we will proceed further on the reported issue and provide you with the appropriate solution at the earliest.


Regards,
Lokesh B


Attachment: Avery5160DevLabels_Modified_(1)_18442bdb.zip

Loader.
Up arrow icon