Aspose.Words and Mail Merge: Query regarding the use of Regions (TableStart / TableEnd) within Word Merge IF statements

We are using the Aspose.Words mail merge methods (MailMerge.Execute / MailMerge.ExecuteWithRegions) to merge data and repeating rows of data into a document from an XML file data source.

For example:

MailMerge.Execute for:

{ MERGEFIELD INCLUDE_TABLE }

MailMerge.ExecuteWithRegions for:

{ MERGEFIELD TableStart:T1 }--{ MERGEFIELD CODE }--{ MERGEFIELD TableEnd:T1 }

{ MERGEFIELD TableStart:T2 }--{ MERGEFIELD CODE }--{ MERGEFIELD TableEnd:T2 }

However, what we would like to do is conditionally merge data from one of the two different tables (either table ‘T1’ or table ‘T2’) depending on a basic Word IF condition. For example, the equivalent of the following pseudo code:

{ IF {MERGEFIELD INCLUDE_TABLE} = "1" "(true = merge rows from T1)" "false = merge rows from T2"}

The example code being:

{ IF { MERGEFIELD INCLUDE_TABLE } = "1" "{ MERGEFIELD TableStart:T1 }--{ MERGEFIELD CODE }--{ MERGEFIELD TableEnd:T1 }" "{ MERGEFIELD TableStart:T2 }--{ MERGEFIELD CODE }--{ MERGEFIELD TableEnd:T2 }" }

For an XML data source of:

<r>
	<INCLUDE_TABLE>1</INCLUDE_TABLE>
	<T1>
		<CODE>T1.1</CODE>
	</T1>
	<T1>
		<CODE>T1.2</CODE>
	</T1>
	<T2>
		<CODE>T2.1</CODE>
	</T2>
	<T2>
		<CODE>T2.2</CODE>
	</T2>
</r>

The expected output would simply be:

–T1.1–
–T1.2–

For an XML data source of:

<r>
	<INCLUDE_TABLE>2</INCLUDE_TABLE>
	<T1>
		<CODE>T1.1</CODE>
	</T1>
	<T1>
		<CODE>T1.2</CODE>
	</T1>
	<T2>
		<CODE>T2.1</CODE>
	</T2>
	<T2>
		<CODE>T2.2</CODE>
	</T2>
</r>

The expected output would simply be :

–T2.1–
–T2.2–

Using the above example, the merge generates the exception: “Mail merge regions ‘T1’ and ‘T2’ overlap. Two regions cannot start/end in the same paragraph or occupy the same table row.”

Is there are recommended way of doing this please – a specific configuration of the IF statement which would support this?

A second (and related) question. Please may we check the purpose for using the MailMergeCleanupOptions.RemoveContainingFields option. We are using it for the merge (which may influence the main question above). Is the following correct?

For the example merge of:

{ IF {MERGEFIELD INCLUDE_TABLE} = "1" "true" "false"}

Where INCLUDE_TABLE = “1”

When ‘RemoveContainingFields’ is used in the merge, the result of the above will simply be ‘true’. The IF statement is removed from the merge document completely and cannot be returned using ALT-F9 to toggle the field code and value. Is this the purpose of the option?

Thanks in advance.

@JamesMurdoch The problem occurs because both regions are defined in the same paragraph. To avoid the exception it is required to put regions in separate paragraphs. For example see the following modified template: in.docx (12.8 KB)
out.docx (12.6 KB)

DataTable t1 = new DataTable("T1");
t1.Columns.Add("CODE");
t1.Rows.Add("T1.1");
t1.Rows.Add("T1.2");

DataTable t2 = new DataTable("T2");
t2.Columns.Add("CODE");
t2.Rows.Add("T2.1");
t2.Rows.Add("T2.2");

DataSet ds = new DataSet();
ds.Tables.Add(t1);
ds.Tables.Add(t2);

Document doc = new Document(@"C:\Temp\in.docx");
doc.MailMerge.Execute(new string[] { "INCLUDE_TABLE" }, new string[] { "2" });
doc.MailMerge.ExecuteWithRegions(ds);
doc.UpdateFields();
doc.Save(@"C:\Temp\out.docx");

Alternatively you can use soft line break and and UseWholeParagraphAsRegion set to false:

Document doc = new Document(@"C:\Temp\in1.docx");
doc.MailMerge.Execute(new string[] { "INCLUDE_TABLE" }, new string[] { "2" });
doc.MailMerge.UseWholeParagraphAsRegion = false;
doc.MailMerge.ExecuteWithRegions(ds);
doc.UpdateFields();
doc.Save(@"C:\Temp\out1.docx");

in1.docx (12.8 KB)
out1.docx (10.0 KB)

Also, to avoid calling doc.UpdateFields(); you can change the order of executing mail merge:

Document doc = new Document(@"C:\Temp\in1.docx");
doc.MailMerge.UseWholeParagraphAsRegion = false;
doc.MailMerge.ExecuteWithRegions(ds);
// Remove empty paragraphs and containing IF field.
doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveEmptyParagraphs | MailMergeCleanupOptions.RemoveContainingFields;
doc.MailMerge.Execute(new string[] { "INCLUDE_TABLE" }, new string[] { "2" });
doc.Save(@"C:\Temp\out2.docx");

out2.docx (9.9 KB)