RemoveEmptyParagraph conflict with RemoveUnusedFields

Depending on which on I execute last, it overrides the previous option. For example…

doc.MailMerge.CleanupOptions = MailMerging.MailMergeCleanupOptions.RemoveEmptyParagraphs
doc.MailMerge.CleanupOptions = MailMerging.MailMergeCleanupOptions.RemoveUnusedFields

The unused merge fields are gone but the empty line in the label remains. See attachment RemoveUnusedFields_Last.png

If I run it the other way with RemoveEmptyParagraphs last like so…

doc.MailMerge.CleanupOptions = MailMerging.MailMergeCleanupOptions.RemoveUnusedFields
doc.MailMerge.CleanupOptions = MailMerging.MailMergeCleanupOptions.RemoveEmptyParagraphs

The blank line in the label is gone but the unused merge fields remain. See attachment RemoveEmptyParagraphs_Last.png

Hi there,

Thanks for your inquiry. Please use MailMerge.DeleteFields method to remove mail merge related fields from the document. This method removes MERGEFIELD and NEXT fields from the document.

Moreover, following code example shows how to use multiple mail merge cleanup options. Please check last code example in following documentation link.
How to Remove Unmerged Fields and Empty Paragraphs during Mail Merge

Hope this helps you. Please let us know if you have any more queries.

doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveEmptyParagraphs;
doc.MailMerge.CleanupOptions |= MailMergeCleanupOptions.RemoveUnusedRegions;
doc.MailMerge.Execute(...);
doc.MailMerge.DeleteFields();
doc.Save(MyDir + "Out.docx");

Worked prefectly. Thanks - but why the “|=” symbol by the “RemoveUnusedFields”?

Hi there,

Thanks for your inquiry. In your case, you need to use multiple cleanup options. Please use bitwise Binary OR Operator as shown below. If you do not use this bitwise operator, the last cleanup options will be used by mail merge engine.

Hope this answers your query. Please let us know if you have any more queries.

doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveContainingFields;
doc.MailMerge.CleanupOptions |= MailMergeCleanupOptions.RemoveStaticFields;
doc.MailMerge.CleanupOptions |= MailMergeCleanupOptions.RemoveEmptyParagraphs;
doc.MailMerge.CleanupOptions |= MailMergeCleanupOptions.RemoveUnusedRegions;
doc.MailMerge.CleanupOptions |= MailMergeCleanupOptions.RemoveUnusedFields;

VB.net does not have a bitwise inclusive OR. Is there some other operator I should use?

Hi there,

Thanks for your inquiry. Please check the last code example in following documentation link.
How to Remove Unmerged Fields and Empty Paragraphs during Mail Merge

Please check following VB.NET code snippet and let us know if you have any more queries.

doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveContainingFields
doc.MailMerge.CleanupOptions = doc.MailMerge.CleanupOptions Or MailMergeCleanupOptions.RemoveStaticFields
doc.MailMerge.CleanupOptions = doc.MailMerge.CleanupOptions Or MailMergeCleanupOptions.RemoveEmptyParagraphs
doc.MailMerge.CleanupOptions = doc.MailMerge.CleanupOptions Or MailMergeCleanupOptions.RemoveUnusedRegions
doc.MailMerge.CleanupOptions = doc.MailMerge.CleanupOptions Or MailMergeCleanupOptions.RemoveUnusedFields