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

Free Support Forum - aspose.com

Perform mail merge with DOC as Data Source using C#

Hello, is there an easy way to use a doc file as a data source for mail merging in Aspose similar to the functionality that exists in MS Word? Right now, I am opening the data file, extracting the first (and only) table, and looping through each row and column and converting the data to a DataTable. Extracting text and handling images is relatively easy but things get complicated when some cells contain tables and others contained mixed data types. I’ve attached a basic example of the types of documents that need to be merged using Aspose.

Can an Aspose.Words.Tables.Table be converted to a DataTable or any other DataSource that can be used when calling MailMerge.Execute()?

Hi Shane,

Thanks for your query. Please use the following code snippet to convert tables in word file to DataTable and use it in mail merge. Please read following documentation links for your kind reference.

Document doc = new Document(MyDir + "data.doc");
Node[] tables = doc.GetChildNodes(NodeType.Table, true).ToArray();
DataSet ds = new DataSet();
foreach (Table table in tables)
Document docTemplate = new Document(MyDir + "template.doc");
docTemplate.Save(MyDir + "out.doc");
public DataTable GetDataTable(Table table)
    DataTable dt = new DataTable();
    char[] escArr = { '\a' };
    foreach (Cell cell in table.FirstRow.Cells)
    for (int i = 1; i < table.Rows.Count; i++)
        DataRow row = dt.NewRow();
        for (int j = 0; j < table.Rows[i].Cells.Count; j++)
            row[j] = table.Rows[i].Cells[j].Range.Text.Trim(escArr);
    return dt;

Thanks for the reply, but this is the code I am already using. If you look at the example documents I posted, you will see the tables within the cells of my data file. The line:

row[j] = table.Rows[i].Cells[j].Range.Text.Trim(escArr);

Converts these tables to plain text. I need to maintain the data as it is in the data file like Word does when merging.

Hi again,
I think I found a solution. I modified the code here https://docs.aspose.com/words/net/insert-and-append-documents/ and used it import the nodes in each cell of mydata file and insert them into the template document at each merge field. I’m basically bypassing the MailMerge and doing it manually. Please let me know if I am over-complicating this and there is an easier way.
Thanks for the help!

Hi there,

Thanks for your inquiry.

That depends, if you want the content inserted while keeping the formatting of the content found in each cell then the current way you are doing things sounds like the best method.

However, if you want the data inserted using the formatting of the destination style (i.e the formatting of the merge field) or as plain text without any formatting then using mail merge would be a much easier option. Tahir provided you with some great code on how to achieve this.