Please attach your document because it all works fine for me.
If you use MailMerge.Execute then you don't need TableStart and TableEnd merge fields in the document. In fact this form of mail merge does not use merge regions and does not touch those fields. Maybe you are getting these field left out because you use this normal mail merge.
It does not matter how many tables you have in your data set. As long as your Table.Name matches the name of the table in TableStart and TableEnd fields, then there will be a match between the merge region and table found and the mail merge will execute and remove the fields from the final document.
I've modified my document:
«TableStart:TableA»«FieldA»«TableEnd:TableA»
«TableStart:TableB»«FieldB»«TableEnd:TableB»
I've modified my code:
[Test]
public void TestEmptyTable()
{
Document doc = TestUtil.Open(
@"MailMerge\TestEmptyTable.doc");
DataSet ds =
new DataSet();
DataTable tableA =
new DataTable("TableA");
tableA.Columns.Add(
"FieldA");
tableA.Rows.Add(
new object[] {"aaa"});
ds.Tables.Add(tableA);
DataTable tableB =
new DataTable("TableB");
tableB.Columns.Add(
"FieldB");
ds.Tables.Add(tableB);
doc.MailMerge.ExecuteWithRegions(ds);
//Customer complained saving to memory stream throws. Does not throw for me.
MemoryStream s =
new MemoryStream();
doc.Save(s, SaveFormat.FormatDocument);
TestUtil.Save(doc,
@"MailMerge\TestEmptyTable Out.doc");
Assert.AreEqual(
"aaa\x000c", doc.GetText());
}
Roman Korchagin
Aspose.Words Team Leader
Aspose Auckland Team