We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

MailMerge and CleanupOptions

Hi there,

First, a hint which might be useful to future users.
I open a document template, and clone that in order to merge multiple results.
Clone, doesn’t include the mailmerge cleanupoptions, and they must be set manually afterwards.:

TemplateDocument = new Aspose.Words.Document(“template.dot”);
TemplateDocument.MailMerge.UseNonMergeFields = true;
TemplateDocument.MailMerge.CleanupOptions =
MailMergeCleanupOptions.RemoveStaticFields |
MailMergeCleanupOptions.RemoveContainingFields |
MailMergeCleanupOptions.RemoveEmptyParagraphs |
MailMergeCleanupOptions.RemoveUnusedFields |

object[] FieldNames, FieldValues;
* Parse data into object arrays here…

Document merge_document = TemplateDocument.Clone();

// Clone doesn’t include these settings…
merge_document.MailMerge.UseNonMergeFields = TemplateDocument.MailMerge.UseNonMergeFields;
merge_document.MailMerge.CleanupOptions = TemplateDocument.MailMerge.CleanupOptions;

// Perform merge.
merge_document.MailMerge.Execute(FieldNames, FieldValues);

Now for the trouble…

The template file includes a mergefield, that explicitly creates an empty newline.
This newline is removed when using Aspose to perform the mailmerge, and using

The MergeCode would be something like this:

{ IF { MERGEFIELD “field1” } <> 0 “\r\nInsert newline above this text: { MERGEFIELD “field2” }” “” }

but look at the attached files, and let me know what you think.
Is there a possible workaround, or do you need to fix this internally?

Thank you,

Hi Jens,

Thanks for your inquiry. This seems to be an expected behavior. The problem occurs when using MailMergeCleanupOptions.RemoveContainingFields and MailMergeCleanupOptions.RemoveEmptyParagraphs together. Since the merge field is a child field of IF field, clean up options “RemoveEmptyParagraphs” and “RemoveContainingFields” are applied. In this case, Aspose.Words first removes field code and then removes empty paragraph. In such case, we suggest you please don’t use “RemoveEmptyParagraphs” option, or add another empty line at the beginning of “true part” of IF field. I hope, this helps.

Best regards,

The trouble with leaving out RemoveEmptyParagraphs is, there are a lot if fields that actually are empty and should be removed, whereas the field in question is not empty, it contains a carrige-return linefeed (\r\n)…

Please look at the file: result_aspose_no_cleanup.pdf and compare it to the result_word_ok.doc result. - This is clearly worse than having cleanup with RemoveEmptyParagraphs.

Hi Jens,

Thanks for your inquiry. How about changing the template document and adding an additional empty line at the beginning of “true part” of IF field. Is this workaround acceptable for you?

Best regards,

That would be a last resort,

We have a couple of thousand templates,
and as you would expect, we don’t have a list of what each template contains.
So we have to go through them one by one, line by line :frowning:

I would much prefer if your mailmerge rutine produced similar results to what word does. I note that you once had a case, where you agreed to remove not only empty lines, but lines contained whitespace only.

I would think your problem lies therein?
Ie.: Removing what are actually not empty paragraphs. (they contain whitespace or in mycase “special charaters like \r\n”)
You might want to introduce a flag that turns this feature of?

I can easilly turn whitespace into empty lines (String.Empty or NULL) on myside of the code.
But figuring out what was empty lines and what lines got turned into empty by Aspose after mailmerge has run, is an order of magnitude harder, if not impossible.
Especially when you consider that the entire paragraph has now been removed.

I will try and modify a template pr. your request to see if that would work.
But as I said, I would really only go this route as a last resort.

Hi Jens,

Thanks for the additional information. We have logged your requirement in our issue tracking system as WORDSNET-10938. Our development team will further look into the details of this problem and we will keep you updated on the progress of this issue. We apologize for any inconvenience.

Best regards,

Just an update.

Doubling up on linebreaks (\r\n\r\n) works.

I have been informed, that simply doubling all linebreaks in code, wouldn’t work.
It works in this isolated case, but if a single line,
has multiple mergefields on the same line, where some of those, add linebreaks,
pseudo: test{mergefield adds single linebreak}test2

the expected result would be

while doubling linebreaks in code would produce:


Hi Jens,

Thanks for the additional information. I have logged your comment in our issue tracking system. We will keep you informed of any developments and let you know once this issue is resolved.

Best regards,

Could you please assist in how to setup a mailmerge in Aspose.Words,

that removes all the empty lines, and produces a result similar to the attached doc ?.

Currently, I use:

document.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveContainingFields |
MailMergeCleanupOptions.RemoveEmptyParagraphs |
MailMergeCleanupOptions.RemoveUnusedFields |

Which produced the attach PDF.
I have tried the code given in


But it makes no difference. (all the paragraphs returned, have no parentnode, and hence can’t be removed.)


data_source.txt file,
the datasource used for merging, opened as utf8.

the template file used to merge with.

the expected result, as produced by Microsoft Word.

the result produced using Aspose MailMerge Execute, with the above specified cleanup options.

Execute is performed using
.Execute(fieldNames: string[], fieldValues: object[]);

I have tried having fieldValues as both a mix of string and decimal types,
and as a pure array of strings. Both produce the same flawed result.

But please comment with a valid workaround or solution.

I will attempt write a small test program, that demonstraits what happens in my products current codebase.

Thank you, Jens.

As promised.

Attached is a simple test program, that demonstraits the issue.
Hi Jens,

Thanks for the additional information. We're working over your most recent queries and will get back to you soon.

Best regards,

Hi Jens,

Thanks for being patient. Regarding WORDSNET-10938, our development team has completed the work on your issue and has come to a conclusion that they won’t be able to implement the fix to your issue. Your issue (WORDSNET-10938) is now closed with ‘Won’t Fix’ resolution. Paragraph with IF field is removed according to MailMergeCleanupOptions.RemoveContainingFields option. You should remove this option to get expected result.

Best regards,