IF Mail merge Group is empty or has elements

Hi there.

We're currently working on adding a few features to our application and Word document templates, and so far everything works perfectly, although I'm curious about how to do the following:

We have a word document that we use as a template on which we run a couple of mail merges as well as a regular mail merge.

Everything works perfectly but with group merges, some of them might be empty and not have any items, yet the whole section for that mail merge is still present, and we would like to be able to remove it from the document if no elements are present. I have seen that you can specify IF conditions inside a Word so certain parts do render or not based on the condition, but I'm not sure if it can be applied to a list of elements and how big the list is. I know a solution could be specifying the start of the section and the end of it in the same way that you define the <<BeginGroup:Name>> and <<EndGroup:Name>>, but I'm not entirely sure of how to do that or if there would be a better way.

What would be the best way to go on about this?


Many thanks in advance.


3 Replies

SA Sivakumar Alagusundharam Syncfusion Team January 15, 2025 05:36 PM UTC

Hi Jorge,

Upon further analyzing your query, we understand your requirement. If the Group Merge field does not have values, the text inside the Group Merge field needs to be removed. We have created a sample to meet your requirement.

We created a template document as described below:
We used an IF field, and the condition for the IF field is a simple field. The name of the simple field is ‘Groupname + Has value’. We set the GroupMergeField in the true part of the IF field, while the false part is empty.

Work done in the sample:

    1. Using these two events, BeforeClearFields and GetNonEmptyGroupName, we gather field names—empty group names in one collection and non-empty group names in another collection.   
    2. Using the collection, we convert it to arrays of field names and field values, then execute the simple field.
    3. Finally, we update the fields and save the document.


Regards,
Sivakumar. A



Attachment: Mail_Merge_3788110b.zip


JM Jorge Mariño Mariño January 20, 2025 08:08 PM UTC

Good evening Sivakumar,

I have taken a look at the sample you've given me and it's almost what I want! It's taught me how to use the IF conditions properly, but the problem is that I don't want the output document containing the IF condition, I would like the output to be the result of the condition being evaluated,

So if the group exists, then the section formatted by the group stays, and the whole IF condition gets removed from the document (alongside the content of the ELSE) whereas if it's false, the condition is removed alongside all of the content with the exception of the ELSE, does that make sense?

I'm not sure if there's even a native or supported way of doing this or if I will have to evaluate these conditions manually.

Many thanks,

Jorge Mariño Mariño



SR Sindhu Ramesh Syncfusion Team January 21, 2025 07:47 AM UTC

Hi Jorge,
Based on the details provided, we understand that your requirement is to prevent the field from being displayed after evaluation. To achieve this, you can unlink the field after updating it. This will display only the result in the document without retaining the field. To do this, retrieve all the fields in the document and unlink them accordingly. We have modified the sample for your reference.

A screenshot of a computer

Description automatically generated

For more detailed information, kindly refer the User Guide:
Working with Fields in .NET Word (DocIO) library | Syncfusion®

Regards,
Sindhu Ramesh.


Attachment: SyncfusionExamples_DocIOExamples_main_MailMergeMailmergewith.NETobjects_f0ee8142.zip

Loader.
Up arrow icon