We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Mailmerge with nested regions and conditional


We have a word template with nested regions. The nested regions are in a conditional based on a field in the parent region. I’ve attached both the template and the result. In the result you’ll see the nested table is duplicated. The nested table should not be duplicated. We use the following code to generate the pdf:

public void MssMergeMultipleTables(byte[] sslicFile, byte[] ssFile, RLTableRecordList ssTables, out byte[] ssMailMergeFile, out bool ssResult, out string ssResultMsg)
    ssMailMergeFile = new byte[] { };
    ssResult = false;
    ssResultMsg = "";

        MemoryStream m = new MemoryStream(ssFile);
        Document doc = new Document(m);

        doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveContainingFields;
        doc.MailMerge.CleanupOptions |= MailMergeCleanupOptions.RemoveEmptyParagraphs;
        doc.MailMerge.CleanupOptions |= MailMergeCleanupOptions.RemoveUnusedRegions;
        doc.MailMerge.CleanupOptions |= MailMergeCleanupOptions.RemoveUnusedFields;

        DataSet ds = new DataSet();
        foreach (RCTableRecord r in ssTables)
            #region Table
            DataTable dt = new DataTable(r.ssSTTable.ssName);

            foreach (RCCellRecord c in r.ssSTTable.ssHeader.ssSTHeader.ssCells)
                dt.Columns.Add(new DataColumn(c.ssSTCell.ssValue, typeof(string)));

            // dt.Columns.Add(new DataColumn("id", typeof(string)));
            // dt.Columns.Add(new DataColumn("name", typeof(string)));
            foreach (RCRowRecord row in r.ssSTTable.ssRows)
                DataRow dr = dt.NewRow();

                foreach (RCCellRecord c in row.ssSTRow.ssCells)
                    dr[r.ssSTTable.ssHeader.ssSTHeader.ssCells[row.ssSTRow.ssCells.CurrentRowNumber].ssSTCell.ssValue] = c.ssSTCell.ssValue;


            #region NestedTable

            // Nested table will only be added if all properties are set
            if (
            !string.IsNullOrEmpty(r.ssSTTable.ssNestedTable.ssSTNestedTable.ssName) &&
            !r.ssSTTable.ssNestedTable.ssSTNestedTable.ssHeader.ssSTNestedTableHeader.ssCells.Empty &&
            !r.ssSTTable.ssNestedTable.ssSTNestedTable.ssRows.Empty &&
            !string.IsNullOrEmpty(r.ssSTTable.ssNestedTable.ssSTNestedTable.ssParentTableRelationColumnName) &&
                var nesteddt = new DataTable(r.ssSTTable.ssNestedTable.ssSTNestedTable.ssName);

                foreach (RCNestedTableCellRecord nestedc in r.ssSTTable.ssNestedTable.ssSTNestedTable.ssHeader.ssSTNestedTableHeader.ssCells)
                    nesteddt.Columns.Add(new DataColumn(nestedc.ssSTNestedTableCell.ssValue));

                // nesteddt.Columns.Add(new DataColumn("id", typeof(string)));
                // nesteddt.Columns.Add(new DataColumn("name", typeof(string)));

                foreach (RCNestedTableRowRecord nestedrow in r.ssSTTable.ssNestedTable.ssSTNestedTable.ssRows)
                    var nesteddr = nesteddt.NewRow();

                    foreach (RCNestedTableCellRecord nestedc in nestedrow.ssSTNestedTableRow.ssCells)
                        nesteddr[r.ssSTTable.ssNestedTable.ssSTNestedTable.ssHeader.ssSTNestedTableHeader.ssCells[nestedrow.ssSTNestedTableRow.ssCells.CurrentRowNumber].ssSTNestedTableCell.ssValue] = nestedc.ssSTNestedTableCell.ssValue;

                ds.Relations.Add("TableRelation", dt.Columns[r.ssSTTable.ssNestedTable.ssSTNestedTable.ssParentTableRelationColumnName], nesteddt.Columns[r.ssSTTable.ssNestedTable.ssSTNestedTable.ssSubTableRelationColumnName], true);


        doc.MailMerge.UseNonMergeFields = true;

        MemoryStream xmlStream = new MemoryStream();
        doc.Save(xmlStream, Aspose.Words.SaveFormat.Docx);
        xmlStream.Seek(0, SeekOrigin.Begin);
        ssMailMergeFile = new byte[xmlStream.Length];
        xmlStream.Read(ssMailMergeFile, 0, (int)xmlStream.Length);

        ssResult = true;
        ssResultMsg = "p1";

    catch (Exception e)
        ssResult = false;
        ssResultMsg = e.Message;

Can anyone tell me what is wrong in the template?

Thanks, Sven

Hi Sven,

Thanks for your inquiry. It seems that you’re using a year old version of Aspose.Words i.e. 15.1.0. We have just released a new version of Aspose.Words for .NET 16.1.0 and suggest you to please upgrade. Hope, this helps. In case the problem still remains, please attach the following resources here for testing:

  • Your expected Word/PDF showing the correct output. Please create it using MS Word.
  • Please create a standalone simplified console application (source code without compilation errors) that helps us reproduce the same problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click ‘Reply’ button that will bring you to the ‘reply page’ and there at the bottom you can include any attachments with that post by clicking the ‘Add/Update’ button.

Best regards,