Hi,
Let’s say I want to generate a batch of 100 invoices, 2 pages per invoice. The template has headers and footers and a different section for each page. I can do a normal mail merge using “Execute” and providing a DataTable of invoice data, which generates a 200 page document as I want.
Now I want to have a table of invoice lines on each invoice. I can create a DataSet with a DataTable for Invoice and DataTable for InvoiceLine and link them with a foreign key relationship. In theory, I can provide that DataSet to ExecuteWithRegions and then use TableStart:InvoiceLine and TableEnd:InvoiceLine in the template to generate the table.
Problem is, I want the invoice information (Invoice No, Date, Customer Name etc) to be outside of a table region. Is that possible? I want the Invoice level to work like a normal mail merge and the Invoice Line level to work as a table region. If I try to put TableStart:Invoice around the whole 2 page template, then I get errors to do with multiple sections. Even if I get rid of the 2nd page and headers/footers for testing purposes, it doesn’t neatly generate 2 page letters for each invoice like a normal mail merge would.
Does Aspose Words offer a solution to what I’m trying to achieve?
This is the .Net version of Aspose.Words.
Thanks
@quantumsys
Could you please clarify if you are using Aspose.Words for Java and provide more details about the errors you are encountering when trying to implement the mail merge with regions?
@quantumsys You can implement this using nested mail merge with regions. Here is a code and template examples that demonstrates the basic technique.
DataSet pizzaDs = new DataSet();
pizzaDs.ReadXml(@"C:\Temp\data.xml");
Document doc = new Document(@"C:\Temp\in.docx");
doc.MailMerge.ExecuteWithRegions(pizzaDs);
doc.Save(@"C:\Temp\out.docx");
data.zip (568 Bytes)
in.docx (58.3 KB)
out.docx (53.0 KB)
Thanks for your reply. This is basically what I have tried already, although it doesn’t work for me as I have headers, footers and a different section for the back page.
@quantumsys Could you please attach your sample template and expected output? We will check it and provide you more information.
Is there a way to provide the template that isn’t public?
@quantumsys It is safe to attach files in the forum. Only you as a topic starter and Aspose staff can access the attachments. Also, I can make the topic private, so only you and Aspose staff can see it.
Here is the template. I have put the nested TableStart and TableEnd in. I have left out the TableStart/TableEnd for the master table (let’s say the table is called “Arrangement”). As I think you’ll see when you look at the template, it’s hard to know where to put the TableStart/TableEnd to include the full content of both pages.
Thanks.
Confirmation of Arrangement.zip (561.5 KB)
@quantumsys Thank you for additional information. To achieve what you need it is required to generate a separate document for each master item and then concatenate the resulting documents. It is required to execute mail merge with regions for each master item and simple mail merge for each master item. For example see the following code:
DataTable master = new DataTable();
master.Columns.Add("Country");
master.Columns.Add("Town");
master.Rows.Add("New Zealand", "Auckland");
master.Rows.Add("Ukraine", "Kharkiv");
List<Document> docs = new List<Document>();
foreach (DataRow dr in master.Rows)
{
// open template and fill it with data.
Document doc = new Document(@"C:\Temp\in.doc");
docs.Add(doc);
// Execute simple mail merge for the given record.
doc.MailMerge.Execute(dr);
// Execute mail merge with regions. Use dummy data for demonstration purposes.
DataTable schedule = new DataTable("Schedule");
schedule.Columns.Add("DueDate");
schedule.Columns.Add("AmountDue");
for (int i = 1; i < 5; i++)
schedule.Rows.Add($"{Random.Shared.Next(1, 28)}.{Random.Shared.Next(1, 12)}.2025", $"{Random.Shared.Next(1, 28)}.{Random.Shared.Next(1, 12)}.2025");
doc.MailMerge.ExecuteWithRegions(schedule);
}
// Concatenate all documents and save the result.
Document result = Merger.Merge(docs.ToArray(), MergeFormatMode.MergeFormatting);
result.Save(@"C:\Temp\out.docx");
1 Like
Thanks for the information. I can see that the concept should work. Say I have 1000 master records. Would there by a significant change in performance between executing a single mail merge compared to executing 1000 mail merges and then merging the docs?
@quantumsys I do not think that there will be significant performance impact, but it is better to test with actual data.
1 Like