How to create table when use MailMerge in aspose.words

Hi all,
I have Document use

Document doc = new Document(TempFolder + "TemplateThongTri.doc");
DataTable dataTable = ConvertToDataTable(SourceNo2);
foreach (var item in SourceNo2)
{
    doc.MailMerge.Execute(
        new string[] { "LoaiThongTri", "TitleThongTri", "TenDonVi", "NamKeHoach", "TenLoai", "TenKhoan" },
        new object[] { "MIAF", "GUD", "TIT", "2015", "460", "468" });
}
DataTable dataTable = ConvertToDataTable(SourceNo2);
doc.MailMerge.ExecuteWithRegions(dataTable);

-------------------------------------------------------------
I want create a table after add field but not working. Help me please
P/S: I use Aspose.words version 10.7.0.0

how to create table when use MailMerge in aspose.words

I use ExecuteWithRegions of Aspose.Words but not working

Hi there,

Thanks for your inquiry. We have tested the scenario using Aspose.Words for .NET 15.11.0 with following simple code example and have not found any issue. Please use Aspose.Words for .NET 15.11.0.

Could you please share some more detail about your requirements? Please manually create your expected Word document using Microsoft Word and attach it here for our reference. We will investigate how you want your final Word output be generated like. We will then provide you more information about your query along with code.

Document doc = new Document(MyDir + "TemplateThongTri.doc");
DataTable dt = new DataTable("dataTable");
dt.Columns.Add("MaTieuMuc", typeof(string));
dt.Columns.Add("MaNganh", typeof(string));
for (int i = 0; i < 10; i++)
{
    DataRow dr = dt.NewRow();
    dr["MaTieuMuc"] = "Test value";
    dr["MaNganh"] = "Test value";
    dt.Rows.Add(dr);
}
doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveContainingFields;
doc.MailMerge.CleanupOptions |= MailMergeCleanupOptions.RemoveStaticFields;
doc.MailMerge.CleanupOptions |= MailMergeCleanupOptions.RemoveEmptyParagraphs;
doc.MailMerge.CleanupOptions |= MailMergeCleanupOptions.RemoveUnusedRegions;
doc.MailMerge.CleanupOptions |= MailMergeCleanupOptions.RemoveUnusedFields;
doc.MailMerge.ExecuteWithRegions(dt);
doc.Save(MyDir + "Out.docx");

Thank you but i use version 10.7.0.0
I think

Row row_ = new Row(doc);
table.AppendChild(row_);
Cell cell_ = new Cell(doc);
cell_.AppendChild(new Paragraph(doc));
cell_.FirstParagraph.AppendChild(new Run(doc, "Mục"));
row_.AppendChild(cell_);
row_.AppendChild(cell_.Clone(false));

Hi there,

Thanks for your inquiry.

Please manually create your expected Word document using Microsoft Word and attach it here for our reference. We will investigate how you want your final Word output be generated like. We will then provide you more information about your query along with code.

Hi There,
Thank for help me,
I have attach file TemplateThongTri.doc
but your example use Aspose.Words for .NET 15.11.0 and i do not have it. I have ver 10.7.
Can you help me with Aspose.Words version10.7.0 and my file template(TemplateThongTri.doc).
now, i do it :

doc.MailMerge.Execute(
    new string[] { "LoaiThongTri", "TitleThongTri", "TenDonVi", "NamKeHoach", "TenLoai", "TenKhoan" },
    new object[] { LoaiThongTri, TitleThongTri, TenDonVi, NamKeHoach, TenLoai, TenKhoan });
Table table = new Table(doc);
doc.FirstSection.Body.AppendChild(table);
Aspose.Words.Tables.Row row = new Aspose.Words.Tables.Row(doc);
table.AppendChild(row);
Aspose.Words.Tables.Cell cell = new Aspose.Words.Tables.Cell(doc);
cell.AppendChild(new Paragraph(doc));
cell.FirstParagraph.AppendChild(new Run(doc, "Mục"));
row.AppendChild(cell);
row.AppendChild(cell.Clone(false));
row.LastCell.AppendChild(new Paragraph(doc));
row.LastCell.FirstParagraph.AppendChild(new Run(doc, "Tiểu mục"));
row.AppendChild(cell.Clone(false));
row.LastCell.AppendChild(new Paragraph(doc));
row.LastCell.FirstParagraph.AppendChild(new Run(doc, "Tiết mục"));
row.AppendChild(cell.Clone(false));
row.LastCell.AppendChild(new Paragraph(doc));
row.LastCell.FirstParagraph.AppendChild(new Run(doc, "Ngành"));
row.AppendChild(cell.Clone(false));
row.LastCell.AppendChild(new Paragraph(doc));
row.LastCell.FirstParagraph.AppendChild(new Run(doc, "NỘI DUNG"));
row.AppendChild(cell.Clone(false));
row.LastCell.AppendChild(new Paragraph(doc));
row.LastCell.FirstParagraph.AppendChild(new Run(doc, "Số tiền"));
foreach (var item in Source)
{
    Aspose.Words.Tables.Row row_ = new Aspose.Words.Tables.Row(doc);
    table.AppendChild(row_);
    Aspose.Words.Tables.Cell cell_ = new Aspose.Words.Tables.Cell(doc);
    cell_.AppendChild(new Paragraph(doc));
    cell_.FirstParagraph.AppendChild(new Run(doc, item.MaMuc));
    row_.AppendChild(cell_);
    row_.AppendChild(cell_.Clone(false));
    row_.LastCell.AppendChild(new Paragraph(doc));
    row_.LastCell.FirstParagraph.AppendChild(new Run(doc, item.MaTieuMuc));
    row_.AppendChild(cell_.Clone(false));
    row_.LastCell.AppendChild(new Paragraph(doc));
    row_.LastCell.FirstParagraph.AppendChild(new Run(doc, item.MaTietMuc));
    row_.AppendChild(cell_.Clone(false));
    row_.LastCell.AppendChild(new Paragraph(doc));
    row_.LastCell.FirstParagraph.AppendChild(new Run(doc, item.MaNganh));
    row_.AppendChild(cell_.Clone(false));
    row_.LastCell.AppendChild(new Paragraph(doc));
    row_.LastCell.FirstParagraph.AppendChild(new Run(doc, item.TenNoiDung));
    row_.AppendChild(cell_.Clone(false));
    row_.LastCell.AppendChild(new Paragraph(doc));
    row_.LastCell.FirstParagraph.AppendChild(new Run(doc, item.SoTien.ToString()));
}

Hi there,

Thanks for your inquiry. Your template document contains mail merge fields with and without regions. You need to call MailMerge.Execute and MailMerge.ExecuteWithRegions in your code example. There is no need to create new rows and append them into table. We suggest you please read following articles for your kind reference.
Simple Mail Merge Explained
How to Execute Simple Mail Merge

Mail Merge with Regions Explained
How to Execute Mail Merge with Regions