Free Support Forum - aspose.com

Labels still now working. Here is my code and word attachment

DataSet being passed in has one table named AddressList and two records...

DataTable addressTable = new DataTable("AddressList");

addressTable.Columns.Add("AddressLine1");

addressTable.Columns.Add("AddressLine2");

addressTable.Columns.Add("City");

addressTable.Columns.Add("State");

addressTable.Columns.Add("Zip");

DataRow dr1 = addressTable.NewRow();

dr1["AddressLine1"] = "test1";

dr1["AddressLine2"] = "";

dr1["City"] = "test";

dr1["State"] = "test";

dr1["Zip"] = "test";

DataRow dr2 = addressTable.NewRow();

dr2["AddressLine1"] = "atest";

dr2["AddressLine2"] = "";

dr2["City"] = "btest";

dr2["State"] = "ctest";

dr2["Zip"] = "ftest";

addressTable.Rows.Add(dr1);

addressTable.Rows.Add(dr2);

labelDS.Tables.Add(addressTable);

string mailingLabelFileName = "C:\\mailingLabels.doc";

byte[] abEventReportFormBytes = this.GetSharePointFile(mailingLabelFileName);

// Do Mail Merge

MailMergerService mms = DocumentServiceFactory.GetDocumentService();

result = mms.MergeNonExcel(abEventReportFormBytes, labelDS, DocumentType.Word, true);

private byte[] MergeNonExcel(byte[] template, DataSet data, DocumentType outputType)

{

Aspose.Word.License wordLicense = new Aspose.Word.License();

wordLicense.SetLicense("Aspose.Custom.lic");

Aspose.Pdf.License pdfLicense = new Aspose.Pdf.License();

pdfLicense.SetLicense("Aspose.Custom.lic");

byte[] result = null;

using (MemoryStream memoryStream = new MemoryStream(template))

{

Document doc = new Document(memoryStream);

Debug.WriteLine("RowCount for Table[0]: " + data.Tables[0].Rows.Count);

if (data.Tables[0].Rows.Count == 0)

return template;

doc.MailMerge.Execute(data.Tables[0]);

for (int i = 1; i < data.Tables.Count; i++)

{

DataTable tbl = data.TablesIdea [I];

doc.MailMerge.ExecuteWithRegions(tbl);

}

result = DocumentToBytes(doc, outputType);

}

return result;

}

Hi,

Note this line of code

doc.MailMerge.Execute(data.Tables[0]);

performs mail merge without regions yet you have a merge region defined in your template. Since the data set contains only one table, the code in the for loop which is supposed to perform mail merge with regions is not reached. Remove the TableStart and TableEnd markers from your template. Take a look at the Mailing Labels demo once more - use of simple mail merge (without regions) + NEXT fields is the best approach to creating labels.

Where am I able to find the Mailing Lables Demo? Thank you.

If you installed Aspose.Word using MSI installer, you should have all the demo projects and documents in C:\Program Files\Aspose\Aspose.Word\Demos.