Mail merge IF statements and Merge Regions (TableStart/End)

Aspose version: Aspose Words.NET version 23.6.0

Please may I ask some advice as to how to correctly write Word mail merge code and the Aspose mail merge code to implement Merge Regions (TableStart/End) within mail merge IF statements.

Ultimately, the requirement is:

  1. Include the merge output from a repeating set of rows in a table (the TableStart/End feature) if an enclosing IF condition is true, and that from another table if the condition is false. There should be minimal whitespace at the start and end of the rows.
  2. Remove any unused regions (if there is a region in the document which does not exist as a table in the data). (MailMergeCleanupOptions.RemoveUnusedRegions)
  3. Remove any containing fields (so the recipient of the merged document cannot toggle the field code (alt-F9) and see the IF condition code. (MailMergeCleanupOptions.RemoveContainingFields)
  4. Remove any empty table rows. (MailMergeCleanupOptions.RemoveEmptyTableRows)
  5. We cannot really switch to using ‘MailMerge.UseWholeParagraphAsRegion = false’ as that would impact the merge of existing documents we have (unless there is a way to identify if the region is being merged in an IF statement and can be turned on and off accordingly)

QUESTION: Is there a way to fulfil the above requirement please?

I will supply a Word document with the test mail merge code, and .NET (6) C# code with my experiments. The C# code has my comments listing the result of each experiment and why it passes or fails. The entry point to the class is PerformExperiments().

My thoughts:

  1. I suspect you can’t have the IF statement and the region on one line (tests 7 and 8) as the merge for the region will be performed – introducing blank rows into the resultant document even when the IF is false and the rows aren’t required.

  2. Therefore, I suspect IF with regions must be formatted multiline (tests 9, 10, 11).

  3. I think experiment ‘4’, restricted to the mail merge format in tests 9, 10, 11, is the closest to an acceptable result – but that does introduce blank lines where the IF statement used to be. Which would be especially noticeable when there are several nested IF statements to cater for the choice of multiple tables.

Using ‘MailMerge.UseWholeParagraphAsRegion = false’ with soft line breaks does help minimise the blank lines. But we would not want to turn that on for all regions – only those with in an IF to cater for this issue.

Is there another way please?

Thanks in advance,

James

IfStatementWithRegions.Destination.In.docx (30.6 KB)

SourceCode.docx (18.1 KB)

@JamesMurdoch Adding MailMerge.UnconditionalMergeFieldsAndRegions = true to experiment 4 fixes the 7th test.

MailMergeCleanupOptions mailMergeCleanupOptions = MailMergeCleanupOptions.None;
mailMergeCleanupOptions |= MailMergeCleanupOptions.RemoveUnusedRegions;
mailMergeCleanupOptions |= MailMergeCleanupOptions.RemoveEmptyTableRows;
doc.MailMerge.CleanupOptions = mailMergeCleanupOptions;
doc.MailMerge.UnconditionalMergeFieldsAndRegions = true;
doc.MailMerge.ExecuteWithRegions(ds);
mailMergeCleanupOptions |= MailMergeCleanupOptions.RemoveContainingFields;
doc.MailMerge.CleanupOptions = mailMergeCleanupOptions;
doc.MailMerge.Execute(ds.Tables["data"].Rows[0]);

out_5.docx (19.8 KB)

Adding MailMerge.UseWholeParagraphAsRegion=false fixed the 8th, but as you have mentioned you cannot use it. And no, unfortunately, there is no way to enable and disable this option in the template. So it is expected that when region inside IF field is filled the whole paragraph is repeated and there are Multiple lines of 'TABLE2 not requested'.

Probably in your case to “hide” paragraphs with IF field start and end you can mark these paragraph as hidden in your template. For example see the modified template (Test 9)
in.docx (30.8 KB)
out_5.docx (19.9 KB)

I marked paragraph ends of paragraph with IF field start and end as hidden. So in the output there is no empty paragraphs if disable displaying hidden marks.

Thank you for your help on this Alexey.

That is an excellent tip to mark the paragraphs on the IF blocks as hidden – a great help - thank you.

Also, thank you for the suggestion of using doc.MailMerge.UnconditionalMergeFieldsAndRegions = true. You are right, that does fix ‘test 7’ for the 4th experiment. However, even without that change, simply changing the IF condition on test 7 from:

{IF{MERGEFIELD USE_TABLE} = “1”

To:

{IF{MERGEFIELD USE_TABLE} < “2”

‘fixes’ the issue too.

As the value of USE_TABLE is 1 in the data, what is the difference between {IF{MERGEFIELD USE_TABLE} = “1” and {IF{MERGEFIELD USE_TABLE} < “2”?

The later produces the merge in the true branch as you would expect, the former does not (this is without you kind suggestion of doc.MailMerge.UnconditionalMergeFieldsAndRegions = true.

Thank you in advance,

James

@JamesMurdoch As I can see conditions behaves the same. I created a simple template and used the following code for testing:

Document doc = new Document(@"C:\Temp\in.docx");
doc.MailMerge.Execute(new string[] { "USE_TABLE" }, new object[] { 1 });
doc.Save(@"C:\Temp\out.docx");

in.docx (12.9 KB)
out.docx (10.1 KB)

Thank you for your time Alexey.

Although I think there is a difference between = “1” and < “2” when there is a region in the true branch.

I have re-uploaded the amended test document (IfStatementWithRegions.Destination.In.docx), with test 7.1 introduced.

It is the same as test 7 (above it) but with = “1” changed to < “2”. I have highlighted both in red.

The code in experiment 4 (in the original uploaded source code) produces a different merge result between test 7 and 7.1: 7 being incorrect, 7.1 being correct (it has the merged rows).

I am struggling to understand the difference - they should produce the same result. Do you have any ideas please?

Thank you,

James

IfStatementWithRegions.Destination.In.docx (31.3 KB)

@JamesMurdoch The answer is simple, {IF {MERGEFIELD USE_TABLE} = “1” is false when {MERGEFIELD USE_TABLE} is not merged, but {IF {MERGEFIELD USE_TABLE} < “2” is true when {MERGEFIELD USE_TABLE} is not merged. You can see this if update field in the document before executing mail merge. Since in experiment 4 mail merge with regions is executed before executing simple mail merge and UnconditionalMergeFieldsAndRegions is disabled in the first case the region is not filled.

Alexey,

Apologies for the late reply. Thank you for all your help on this Alexey.

Yes – I understand your point – it is because the merge of regions is before the main merge. That being, with UnconditionalMergeFieldsAndRegions disabled, the region merge is checking the parent IF and, as you say, IF{MERGEFIELD USE_TABLE} = “1” will be false as the main merge hasn’t happened yet. So, it looks like you have to enable UnconditionalMergeFieldsAndRegions - if the merge of regions happens before the main merge and if the document is going to have an IF which contains regions.

Except however, if the IF statement + merge region is not on the same line – then the result is different - the difference between test 7 and 7.2 when using experiment 4:

In document: IfStatementWithRegions.Destination.In.2.docx
Out document: IfStatementWithRegions.Destination.Out.2.Experiment_4.docx

I guess in this instance, the region has become ‘disconnected’ from its enclosing IF somehow and the IF is not checked, whereas on a single line it is.

I guess that is something for us (here) just to be aware of.

Thanks for your help again Alexey.

IfStatementWithRegions.Destination.In.2.docx (31.4 KB)

IfStatementWithRegions.Destination.Out.2.Experiment_4.docx (20.3 KB)

@JamesMurdoch You are right it is a strange behavior. We will further investigate it.

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-26918

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Thank you for looking into this Alexey.

Regards,

James

1 Like