Force insert of new rows while mail merge

Hi,

i would like to force the creation of new table rows during mail merge.

When I have a table with two or more columns and the TableStart is in a different cell than the TableEnd, then while mail merge there will be new table rows inserted for each placeholder.

When I have a table with one column (or more) and have TableStart and TableEnd in the same cell, there won’t be rows inserted while mail merge.

Is there a way to force the row insert while mail merge in a region?

In the Word examples (attachment), you see the template and the results. I would like that the first table has also 10 rows after mail merge.

Kind regards
Peter
Word Example.zip (17.5 KB)

@GEDAT

In your case, we suggest you following solution.

  • Implement IFieldMergingCallback interface.
  • In IFieldMergingCallback.FieldMerging, please use Node.GetAncestor method to get the ancestor row, clone it and append it to the table.
  • Move the cursor to the first cell of cloned row and insert your desired content.

We suggest you please read the following articles.
How to Apply Custom Formatting during Mail Merge
Move the Cursor

Please check the following code snippet. Hope this helps you.

private class HandleTableRows : IFieldMergingCallback
{

    void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
    {
        DocumentBuilder builder = new DocumentBuilder(e.Document);
        builder.MoveToMergeField(e.DocumentFieldName);

        Row row = (Row)e.Field.Start.GetAncestor(NodeType.Row);
        if (row != null)
        {
            row.ParentTable.Rows.Add(row.Clone(true));
            builder.MoveTo(row.ParentTable.LastRow.FirstCell.FirstParagraph);
            builder.Write(e.FieldValue.ToString());
        }
        e.Text = "";
    }

Hi, thank you for the answer.

shouldn’t it look like this?

void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
{
    DocumentBuilder builder = new DocumentBuilder(e.Document);
    Row row = (Row)e.Field.Start.GetAncestor(NodeType.Row);
    if (row != null)
    {
        builder.MoveToMergeField(e.DocumentFieldName);
        row.ParentTable.Rows.Add(row.Clone(true));
        builder.MoveTo(row.ParentTable.LastRow.FirstCell.FirstParagraph);
        builder.Write(e.FieldValue.ToString());
    }
    e.Text = "";
}

The problem is, if there are 10 Values, there will be 11 rows. And the first row ist empty.
How to remove it and when?

Example Placeholder.zip (9.5 KB)

@GEDAT

Yes, your code is correct. You can remove the first row by using Row.Remove method.

table.Rows[0].Remove()

But when and where should I do that?

There is e.RecordIndex. Can I get the Record Count somehow, so I could remove the row after the last record was processed?

@GEDAT

You cannot get the record count of data source using Aspose.Words. In your case, you do not need to clone the row for the first record. Please clone the row for second record to get the desired output.

I tried this:

void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
{
    DocumentBuilder builder = new DocumentBuilder(e.Document);
    Row row = (Row)e.Field.Start.GetAncestor(NodeType.Row);
        if (row != null)
        {
            builder.MoveToMergeField(e.DocumentFieldName);
            if (e.RecordIndex > 0)
                row.ParentTable.Rows.Add(row.Clone(true));
            builder.MoveTo(row.ParentTable.LastRow.FirstCell.FirstParagraph);
            builder.Write(e.FieldValue.ToString());
        }
        e.Text = "";
}

but this is the result:
grafik.png (28.2 KB)

@GEDAT

Please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing. We will investigate the issue and provide you more information about your query along with code example.