Copy/Paste within doc to handle nested mail merge

My application needs nested mail merging (repeating sections within repeating sections), so I gather I cannot use the mail merge that comes with Aspose.Words. I am trying to do this operation by directly manipulating the document. However, I am finding it difficult to simply copy a range of the document (e.g., delineated by a bookmark) from one point to another. The code to do this seems complex (for example, if the range of text contains paragraphs or tables, you need to split the insertion point paragraph and insert clones of the nodes to copy either within the insertion point paragraph or create new paragraphs). Is there an easier way to do this?

Hi
Thanks for your request. I think that you can achieve mail merge with nested data using MergeField event handler. For example see the following code and attached documents.

public void Test172()
{
    // Open first template
    Document doc = new Document(@"Test172\mainTemp.doc");
    // Add MergeField event handler
    doc.MailMerge.MergeField += new MergeFieldEventHandler(MailMerge_MergeField_InsertNestedData);
    // Get data and execute mail merge
    DataTable table = GetFirstDataSource();
    doc.MailMerge.ExecuteWithRegions(table);
    // save output document
    doc.Save(@"Test172\out.doc");
}
void MailMerge_MergeField_InsertNestedData(object sender, MergeFieldEventArgs e)
{
    if (e.FieldName == "PlaceHolder")
    {
        // Open second template
        Document doc = new Document(@"Test172\nestedTemp.doc");
        // get data
        DataTable table = GetNestedDataSource();
        // execute mailmerge 
        doc.MailMerge.ExecuteWithRegions(table);
        // Insert nested data
        InsertDocument(e.Field.Start.ParentParagraph, doc);
        e.Text = string.Empty;
    }
}
private DataTable GetFirstDataSource()
{
    DataTable table = new DataTable("myTable");
    table.Columns.Add("Name");
    table.Columns.Add("Company");
    // This coulumn is needed to insert neted table
    table.Columns.Add("Placeholder");
    // Add rows
    for (int i = 0; i < 10; i++)
    {
        table.Rows.Add(new object[] { "name_" + i.ToString(), "company_" + i.ToString(), "" });
    }
    return table;
}
private DataTable GetNestedDataSource()
{
    DataTable table = new DataTable("nestedTable");
    table.Columns.Add("item1");
    table.Columns.Add("item2");
    table.Columns.Add("item3");
    // Add rows
    for (int i = 0; i < 10; i++)
    {
        table.Rows.Add(new object[] { "item_1." + i.ToString(), "item_2." + i.ToString(), "item_3." + i.ToString() });
    }
    return table;
}

You can find InsertDocument method here:
https://docs.aspose.com/words/java/insert-and-append-documents/
Hope this could help you.
Best regards.

The issues you have found earlier (filed as 39) have been fixed in this update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(61)