Merging fields on a table

attached is a test project.
I am trying to read field list and value list from a file, create a new row on the table, then merge the fields.
however, all sorts of bad things happen -
the field name isn’t found(but it’s there), and in the resulting document (result.docx), it’s actually changed.
Obviously, I’m doing something (37.0 KB)


Thanks for your inquiry. Please note that DocumentBuilder.MoveToMergeField method (String) moves the cursor to a position just beyond the specified merge field and removes the merge field.

You are moving the cursor to the mail merge field and writing the content. If you want to perform a mail merge operation for a single records, we suggest you please use MailMerge.Execute method (String[], Object[]).

Moreover, we suggest you please read about mail merge with regions from here:
Mail Merge with Regions Explained
How to Execute Mail Merge with Regions

If you still face problem, please share some detail about your use case and expected output document. We will then provide you more information about your query.

PS : ZIP your expected output document and upload it.

I have modified the code to use mailmerge.execute.
note : using regions is not an option at this time, as we are attempting to convert our user sites to move from word to Aspose, and there are thousands of .dot files out there and in current use.

included in the zip file is ‘expected.docx’, which was generated using word.

the first table should look the same in both versions.
instead, 3 rows didn’t get added, and the first cell in each row is (43.7 KB) (44.2 KB)
This project should be showing the exact same results as the ‘expected’ results
note 1 : if you comment out the asposedocument.mailmerge.execute, then the tables will have the correct number of lines.
note 2: the first column in the first table is incorrect after the cloned row is inserted.


Thanks for sharing the detail. We are investigating this issue and will get back to you soon.


Thanks for your patience. After investigation, we have noticed that Table.InsertBefore does not insert clone row after calling MailMerge.Execute. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-15673. You will be notified via this forum thread once this issue is resolved. We apologize for your inconvenience.

You are calling MailMerge.Execute method multiple times to create table rows. It is not good approach to perform mail merge. If your data source have huge records, you will face the performance issue. We suggest you please read the mail merge articles shared in my previous post and update your template document.

Moreover, you can also create table by iterating through records as you are currently doing in your application. Please move the cursor to the mail merge field or desired location and insert the content. Please read following articles.
Using DocumentBuilder to Modify a Document Easily
Use DocumentBuilder to Insert Document Elements
Inserting a Table using DocumentBuilder

so, I have reverted the code to what it was to begin with (which uses document builder) and of course the results are still not ‘right’

is there a way to ‘add’ regions to a row programatically? Then maybe merge with regions… (46.0 KB)


Thanks for your patience. Please note that the issue you were facing is not a bug. So, we have closed this issue (WORDSNET-15673) as ‘Not a Bug’.

Please note that after calling MailMerge.Execute the table object will be different. Please use following modified code to get the correct output.

Document doc = new Document(MyDir + "");
DocumentBuilder builder = new DocumentBuilder(doc);

string dataList = File.ReadAllText(MyDir + "dvalues.txt");
string fieldList = File.ReadAllText(MyDir + "dfields.txt");
string[] rowData = dataList.Split('|');
string[] fList = fieldList.Split((char)24);

Table table = doc.FirstSection.Body.Tables[1];

// Clone template row.
Row newRow = (Row)table.Rows[2].Clone(true);

for (int y = 0; y < rowData.Length; y++)
    string[] colData = rowData[y].Split((char)24);
    doc.MailMerge.Execute(fList, colData);

    // Get table after MailMerge
    table = doc.FirstSection.Body.Tables[1];
    table.InsertBefore(newRow, table.LastRow);

doc.Save(MyDir + "result.docx");

glad it works in the sample.

however, my real life code uses the document builder logic (the first one and the last one that I sent), and the data is coming from delphi application.

I don’t think I can use the mailmerge execute, I’m not sure it would correctly work, because I often have the same field name located in different tables.

is there a ‘simple’ way of adding/removing a region to a row?

also, I think there might be something ‘wrong’ with the tables themselves, though it works fine in the 'not aspose (uses word) ’ application, because I’m using the same logic on multiple tables in multiple templates, and it is working correctly in those other places.


Thanks for your inquiry. In this case, we suggest you please create the table using DocumentBuilder. We also suggest you please consider mail merge with regions. Hope this helps you.

  1. I am using the same logic on hundreds of other templates, and the logic is working on the other templates.
  2. I can’t create the table using document builder, as the table already exists on the templates which are designed by our end users.

is there a way to ‘add’ a region when I add a row, then remove it after I am done with the merge?


Thanks for your inquiry. You do not want to create table using DocumentBuilder and mail merge. Unfortunately, your question isn’t clear enough therefore we request you to please elaborate your inquiry further by providing complete details of your usecase. This will help us to understand your scenario, and we will be in a better position to address your concerns accordingly.

attached is the code/and result, of those changes.
it still isn’t working correctly.

in our case, we have dozens of sites. Each site has hundreds of templates that have been designed by our end users over the years.

the templates are constructed in such a way that I use the tables and fields in the tables, to build the query that is run and then used to fill the table. Thus, I CANNOT create the table, it is custom defined by our end users.

and one of the things they like to do is to include the same data, but different parts of that data, in multiple tables (see new attached .dot file) (49.9 KB)


Thanks for your inquiry. You are facing the expected behavior of Aspose.Words. The ffields are inside first and second tables. So, the first call of MailMerge.Execute merges the fields in second table also.

In this case, you need to clone the row in both tables. Please let us know if you have any more queries.

No more questions. thank you for your time.