Help with Avery file folder labels

Hello,

I am using an Avery template, and trying to export data from .net (C#) using Mailmerge.Executewithregions. I have followed the other posts in this forum and it did not work for me. I wanted to know the mistake I am doing here. I have also attached the template used and the code I used to export the data to template is

dataList.Add("test report", ds.Tables[0]);

foreach (KeyValuePair<string, DataTable> dt in dataList)
{
     doc.MailMerge.ExecuteWithRegions(dt.Value);
} 

Any help is greatly appreciated.

Thanks

Can I get any help on this? I need to do this.

Hi,

Thanks for your query. In your case, I suggest you please use simple mail merge as used in Mailing Labels demo. I have attached the template document of Mailing Labels Demo with this post. The shared code shows that you are calling ExecuteWithRegions in foreach loop. If you want to use mail merge with regions, please add all DataTables in one DataSet and then call ExecuteWithRegions method.

Please read the detail of simple mail merge, mail merge with region and mail merge with nested regions from following documentation links.
https://docs.aspose.com/words/net/mail-merge-and-reporting/
https://docs.aspose.com/words/net/types-of-mail-merge-operations/
https://docs.aspose.com/words/net/nested-mail-merge-with-regions/

Thanks for your reply.

I still have an issue with this. I changed the template to the one I attached with this message. As per your suggestion, I removed the regions and now I am using simple mail merge but the same label is repeated (Same data) for 12 times as there are 12 labels in the template.

I removed foreach and used following code:

protected SortedDictionary<String, DataTable> dataList = new SortedDictionary<String, DataTable>();
dataList.Add("Test Avery Report", ds.Tables[0]);

WordExportStream = new System.IO.FileStream(path + "\\" + TemplateFileName + ".doc", System.IO.FileMode.Open);

Aspose.Words.Document doc = new Aspose.Words.Document(WordExportStream);
doc.MailMerge.Execute(dataList["Test Avery Report"].DataSet.Tables[0]);
doc.Save(path + @"\" + TemplateFileName + "OUT.doc");
WordExportStream.Close();

Thanks for your help.

Hi,

Thanks for your inquiry. I have modified your template document. Please find it in attachment. I have replace { MERGEFIELD NEXT } with { Next } filed. Please read more detail about Next filed from here:
http://office.microsoft.com/en-gb/word-help/field-codes-next-field-HP005186174.aspx

Moreover, please use the MailMerge.DeleteFields method. This method removes MERGEFIELD and NEXT fields from the document.

Document doc = new Document(MyDir + "TestAveryUpdated_modified.doc");
doc.MailMerge.Execute(table1);
doc.MailMerge.DeleteFields();
doc.Save(MyDir + "out-new.docx");

Please use the attached modified document and let us know how it goes on your side. Hope this helps you.

Thank you so much. It worked for me.

An improvement on this, in the template I have attached previously, there are 12 labels in one page of Word Template. If I have a Data table of one row, How can I Export to label at a particular position dynamically, say 7th label.

More clearly, How can I dynamically export the data to seventh position leaving the other empty. One way is by having 12 different templates which I feel is not a correct way in my opinion. Is it possible by using IF condition in word document and sending the label number from C#

Thanks for all the help.

Hi,

Thanks for your inquiry. Yes, you can use IF field with Mail Merge field to achieve your requirements. You just need to modify the mail merge fields as shown below.

{ NEXT * MERGEFORMAT }
{IF “{ MERGEFIELD insertLabel }”=“1”
“Name: {MERGEFIELD Number } Address: {MERGEFIELD Address }”
“” * MERGEFORMAT }

I have inserted a field in DataTable named “insertLabel” to use mail merge in IF field. I have attached the modified template with this post. Please see the following code snippet for your kind reference. Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "TestAveryUpdated_modified.doc");
DataTable table1 = new DataTable();
table1.Columns.Add("Number", typeof(string));
table1.Columns.Add("PILastName", typeof(string));
table1.Columns.Add("insertLabel", typeof(string));
table1.Rows.Add("Name 1", "Address 1", "0");
table1.Rows.Add("Name 2", "Address 2", "0");
table1.Rows.Add("Name 3", "Address 3", "0");
table1.Rows.Add("Name 4", "Address 4", "0");
table1.Rows.Add("Name 5", "Address 5", "0");
table1.Rows.Add("Name 6", "Address 6", "1");
table1.Rows.Add("Name 7", "Address 7", "1");
table1.Rows.Add("Name 8", "Address 8", "0");
doc.MailMerge.Execute(table1);
doc.MailMerge.DeleteFields();
doc.Save(MyDir + "out.doc");