Mail Merge Clean Up Options combining REMOVE_CONTAINING_FIELDS and REMOVE_UNUSED_FIELDS

Hi


We are attempting to merge a template that contains if statements and merge fields which may or may not be merged with data.

For example

{ IF { MERGEFIELD Sex } = “M” "
{ MERGEFIELD Address_Line_1 }
{ MERGEFIELD Address_Line_2 }
{ MERGEFIELD Address_Line_3 }
" “” }

Is then merged with the following data

Sex='M’
Address_Line_1=‘Yellow Brick Road’
Address_Line_2=’'
Address_Line_3='Somewhere over the rainbow’

We want the following produced.

Yellow Brick Road
Somewhere over the rainbow

Notice the removal of the Address_Line_2 from the produced document as well as the IF statement being removed.

Currently we get.

Yellow Brick Road

Somewhere over the rainbow

We are using Aspose.Words 14.2.0 and calling it in the folllowing manner.

template.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS | MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS);
template.getMailMerge().execute(customDataSource);

In our customDataSource the getValue method is currently implemented as follows

public boolean getValue(final String fieldName, final Object[] fieldValue)
{
final String value = this.data.get(this.recordIndex).getValue(fieldName);
fieldValue[0] = value;
return null != value;
}

Any help would be appreciated.

Regards

<!–[if supportFields]> IF MERGEFIELD Sex = “M” "

MERGEFIELD Address_Line_1

MERGEFIELD Address_Line_2

MERGEFIELD Address_Line_3

MERGEFIELD Address_Line_4

MERGEFIELD Address_Line_5

" ""


Hi Stuart,


Thanks for your inquiry. In this case, empty paragraphs that result from merging fields with no data needs to be also removed from the document. For this, please add MailMergeCleanupOptions.RemoveEmptyParagraphs option in your code. I hope, this helps.

Best regards,

Hi,


Thank you for the help.

I’ve now added in REMOVE_EMPTY_PARAGRAPHS as well. And it works great for the address example when not included in an IF statement but when an IF statement is included it still doesn’t remove the empty address lines.

So in the clean up options we are now specifying

template.getMailMerge().setCleanupOptions(
MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS |
MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS |
MailMergeCleanupOptions.REMOVE_EMPTY_PARAGRAPHS)

We also have a second example where the IF statement includes a new line and a table, which the addition of REMOVE_EMPTY_PARAGRAPHS flag mean the new line is removed. In our template there is a table outside the IF statement so the two tables end up butted together.

I have attached the templates and produced documents for all examples. Files ending ‘_template’ are the templates. Files ending ‘_result’ are what Aspose.Words produced. Files ending ‘_using_word’ are what a Microsoft Word mail merge produced.

The address_line example works correctly in Aspose.

The IF_statement_and_address is the same as the address_line example except where the address lines are wrapped in an IF statement which evaluates to true.

The IF_statement_and_table is where the IF statement includes a new line and a table, but the Aspose.Words produced result removes the new line causing the table to be butted against a table outside the IF statement.

Are we using the flags incorrectly or is there an issue in Aspose.Words as we are currently not able to get the same behaviour as Microsoft Word. Currently to allow a migration from Word to Aspose we require the templates to produce the same result in both.

Regards

Stuart

Hi Stuart,


Thanks for the additional information…
Stuart:
I’ve now added in REMOVE_EMPTY_PARAGRAPHS as well. And it works great for the address example when not included in an IF statement but when an IF statement is included it still doesn’t remove the empty address lines.

So in the clean up options we are now specifying

template.getMailMerge().setCleanupOptions(
MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS |
MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS |
MailMergeCleanupOptions.REMOVE_EMPTY_PARAGRAPHS)
I tested the scenario and have managed to reproduce the same problem on my side. For the sake of correction, I have logged this problem in our issue tracking system as WORDSNET-9921. Our development team will further look into the details of this problem and we will keep you updated on the status of correction. We apologize for your inconvenience.
Stuart:
We also have a second example where the IF statement includes a new line and a table, which the addition of REMOVE_EMPTY_PARAGRAPHS flag mean the new line is removed. In our template there is a table outside the IF statement so the two tables end up butted together.
In this case, you should only specify the MailMergeCleanupOptions.RemoveContainingFields option. I hope, this helps.

Best regards,

Hi Awais


Thank you for raising the issue in regards to REMOVE_EMPTY_PARAGRAPHS within an IF statement.

In regards to the table included inside the IF statement.
Awais:
In this case, you should only specify the MailMergeCleanupOptions.RemoveContainingFields option

However, we can't remove the REMOVE_EMPTY_PARAGRAPHS as
1. We have a single generic piece of code for performing the mail merge as we allow our clients to have custom templates.
2. They have templates that include addresses and tables within IF statements.

These templates did work in Word, are we correct that Aspose.Words mail merge should work the same as the Microsoft Word mail merge and that any inconsistencies between the two should be considered as issues.


Hi Stuart,


Thanks for the additional information. I tested the scenario and have managed to reproduce the same problem on my side. For the sake of correction, I have logged this problem in our issue tracking system as WORDSNET-9932. Our development team will further look into the details of this problem and we will keep you updated on the status of correction. We apologize for your inconvenience.

Best regards,

Hi Stuart,


Thanks for being patient. On further investigation our development team came to know that they won’t be able to implement the fix to your issue. Your issue (WORDSNET-9932 is now closed with ‘Won’t Fix’ resolution/status. The current behavior of Aspose.Words is correct.

Considering the following structure of document, IF field is updated after mail merge. Since the merge field is a child field of IF field, cleanup options “RemoveEmptyParagraphs” and “RemoveContainingFields” are applied. In this case, Aspose.Words first removes field code and then removes empty paragraph.

<w:fldChar
w:fldCharType=
“begin”/> // start of IF field
IF {MERGEFIELD Sex}






<w:fldChar
w:fldCharType=
“end”/> // end of IF field

In such case, we suggest you please don’t use “RemoveEmptyParagraphs” option, or add empty line before table start in “true part” of IF field.

Best regards,

Thanks for the response in regards to WORDSNET-9932.


Has there been any movement on WORDSNET-9921?

Hi Stuart,


Thanks for your inquiry. Unfortunately, WORDSNET-9921 is not resolved yet. This issue is more complex than we initially estimated. I have verified the status of this issue from our issue tracking system and regret to share with you that this issue has been postponed till a later date. We will notify you via this thread as soon as this issue is resolved. We apologize for your inconvenience.

Best regards,

Please can you provide an update on this issue?

Thanks
Stephen

Hi Stephen,


Thanks for your inquiry. Unfortunately WORDSNET-9921 is not resolved yet. The implementation of this issue is still postponed. We will inform you as soon as this issue is resolved. We apologize for your inconvenience.

Best regards,

Hi. Please could you provide an update on when this issue is likely to be fixed. We have been waiting for some time now without any sign of progress on this issue.

Hi Stephen,


Thanks for your inquiry. Unfortunately, your issue is not resolved yet. We have asked the ETA of this issue from our development team and will update you as soon as any estimates are available. We apologize for your inconvenience.

Best regards,