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

Free Support Forum - aspose.com

Removing unused Merge sections


We are usnig Aspose.Words functionality in our project. Attached is a part of the template document which we use. We fill it with data at the run time in C# code using Aspose functionality. Everything works as expected.

If you check the attached template which is basically a word document, you will find many merge fields, merged tables in it. We replace these merge fields/tables with actual data at runtime.

Now our query:
Consider table in RED (Physical Occupancy): Sometimes no data will come in this table while there can be data in other table Financing (In Blue) and vice-versa can

also happen. So we want to get rid of the merge items where there is no data.

I am using the following line of code.

_builder.Document.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveUnusedRegions;

some other code


But the problem here is, it will remove the other merge section also which have data.

I got this information in the aspose documentation.

The MailMergeCleanupOptions.RemoveUnusedRegions flag will remove any region in the document which is not found in the current data source.
If you are merging data from many data sources by using separate calls to MailMerge.ExecuteWithRegions then you need to make sure that this flag is only enabled with the very last merge. Otherwise all unused regions will be removed from the document before they can be merged.

So can you help me how to handle the scenarios where we have multiple data sections and we have to remove only those sections which are unused/or which dont have any data? Or might be any way where we can remove the merge fields or show any dummy/sample information which says data is not available.

Thanks in Advance…!!

Hi there,

Thanks for your inquiry. Its seems that you are calling mail merge operation multiple times. If this is the case, It is best approach to use MailMerge.CleanupOptions property before the last call of mail merge operation. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");

doc.MailMerge.ExecuteWithRegions(data source);

doc.MailMerge.ExecuteWithRegions(data source);

doc.MailMerge.CleanupOptions |= Aspose.Words.Reporting.MailMergeCleanupOptions.RemoveEmptyParagraphs;

doc.MailMerge.CleanupOptions |= Aspose.Words.Reporting.MailMergeCleanupOptions.RemoveUnusedRegions;

doc.MailMerge.CleanupOptions |= Aspose.Words.Reporting.MailMergeCleanupOptions.RemoveUnusedFields;

doc.MailMerge.CleanupOptions |= Aspose.Words.Reporting.MailMergeCleanupOptions.RemoveContainingFields;

doc.MailMerge.ExecuteWithRegions(data source);

<!–[if gte mso 10]> /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin-top:0cm; mso-para-margin-right:0cm; mso-para-margin-bottom:10.0pt; mso-para-margin-left:0cm; line-height:115%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-fareast-language:EN-US;}


Moreover, I suggest you please read following documentation link for your kind reference.

If you still face problem, please share following detail for investigation purposes.

  • Please attach your input Word documents.
  • Please

    create a standalone/runnable simple application (for example a Console
    Application Project
    ) that demonstrates the code (Aspose.Words code) you used to generate
    your output document

  • Please attach the output Word file that shows the undesired behavior.
  • Please
    attach your target Word document showing the desired behavior. You can
    use Microsoft Word to create your target Word document. I will
    investigate as to how you are expecting your final document be generated

it is difficult to say what the problem is without the Document(s) and
simplified application. We need your Document(s) and simple project to
reproduce the problem. As soon as you get these pieces of information to
us we’ll start our investigation into your issue.


1. If I place the code before last ExecuteWithRegions, it dont have any effect, I mean nothing get removed. Please note, we even have Populate MergeField function used at many places. In that case, where should we keep the removing options code?

2. Also, if I mention it at the first place, everything else, which even have data gets removed.

Hi there,

Thanks for your inquiry.

it is difficult to say what the problem is without the Document(s) and
simplified application. We need your Document(s) and simple project to
reproduce the problem. As soon as you get these pieces of information to
us we’ll start our investigation into your issue.

Hi Tahir,

I am really not sure how I am going to attach it in some application. Ours is a big application. Extracting this particular part and reproducing same issue there would be a difficult task. I am also not sure how to proceed here.

But this seems a major issue at our side, so we need a solution for this.
Any idea if anyone else has got the same issue and how they approached it.

Hi there,

Thanks for your inquiry. In your case, I suggest you please try the MailMerge.DeleteFields method. This method removes mail merge related fields from the document.

This method could be useful if your mail merge operation does not always need to populate all fields in the document. Use this method to remove all remaining mail merge fields.

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

Bingo, it worked…!!
Thanks a ton for this.

I think someday, I need to go through all the documents of Aspose :slight_smile:

Hi there,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.