We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Conditional sections (if/else) in mail merge

Hi there,
Some time ago I used DocIO to build DOCX generator based on SQL table. Basically I'm using mail merge - WordDocument.MailMerge.ExecuteGroup() and WordDocument.MailMerge.Execute()

I got a question: is it possible to hide/show a section of DOCX based on data in my SQL?

Let's say I have these columns in my first table: name, surname, is_company and a secondary table called company_details

If field is_company is equal to true (or 1) I'd like to show secondary and fill it using Execure Group, if it is equal to false (or maybe to 0) I'd like to show "Not a company" paragraph.

Docentic has a solution that supports if/else blocks in word mail merge (http://www.docentric.com/documentation/examples/conditional-content), but I'd like to use Your awesome library.

If You need any additional informations please let me know.

8 Replies

PR Poorani Rajendran Syncfusion Team September 10, 2019 07:13 PM UTC

Hi Tomasz,

Thank you for contacting Syncfusion support.

To meet your requirement, we suggest you BeforeClearFieldEventHandler to set the field value if the field value is empty or null for the field. We have prepared a sample application for the same and it can be downloaded from the below link:
https://www.syncfusion.com/downloads/support/forum/147391/ze/GenerateWord603663313

Note: Please find the input Word document in the “Data” folder of above sample application.

Please refer the below UG documentation link to know more about working with mail merge events in Word document using DocIO:
https://help.syncfusion.com/file-formats/docio/working-with-mailmerge?cs-save-lang=1&cs-lang=csharp#beforeclearfield-event

If we misunderstood any of your requirement, kindly provide us your input Word document along with modified code snippet of above sample application and provide details about your complete end requirement, thereby we will analyze the feasibility further on the mentioned requirement and update you with the appropriate solution at the earliest.

Please let us know if you have any other questions.

Regards,
Poorani Rajendran



TJ Tomasz Jagusz September 13, 2019 10:01 AM UTC

Hi Poorani,

sorry for not writing my case clearly.
I've attached sample showing what I need.

Basically I want to hide the whole section of the document if a specific requirement isn't neede.
When you click on "Generate V1" You will get document having two tables - Products and Users, but when you click "Generate V2" Users table will be empty.
I'd like to hide whole section starting with "<if ThereAreUsers>" and ending with "<elseif ThereAreUsers>" when a specific condition is met - field ThereAreUsers is set to false (line 366).

Is this possible?
Please take a look at docentic, I'm looking for similar feature - conditional content.

Hope this issue is more clear right now :)


Attachment: GenerateWordIfElse_eacf849e.zip


PR Poorani Rajendran Syncfusion Team September 16, 2019 11:14 AM UTC

Hi Tomasz,

Thank you for your update.

To meet your requirement, we suggest you set the merge field (“<<ThereAreUsers>>”) within the IF field in the template Word document and  it will be updated based on the merge field result. To update the fields in Word document, we suggest you use UpdateDocumentFields API. Please find the below Microsoft Word application screenshot to insert the merge field within the IF field (Right click on the Field, select Toggle Field Codes or ALT+F9 shortcut key).
In the given sample, we have found that you are setting ClearFields property as false to preserve the unmerged fields as it is in the Word document. We have found that you are executing mail merge for Word document using Execute API(“<<FirstName>>”) as first execution. It performs mail merge with all the fields in the Word document and preserved the unmatched fields based on ClearFields property.

In your case, if there is no value for merge field (“ThereAreUsers”) within IF field, then merge field preserved as unmerged field. This will be reflected in updating IF field expression. So, we suggest you set ClearFields property as true and also recommend to perform ExecuteGroup  as first execution before calling Execute API. Thereby, mail merge is executed for particular group in first time execution and then executed for remaining merge fields in whole Word document (fields without any group). We have modified the given sample application to achieve your requirement and it can be downloaded from the below link:
https://www.syncfusion.com/downloads/support/forum/147391/ze/GenerateWordIfElse1484679146

Please let us know if you have any other questions.

Regards,
Poorani Rajendran.



TA Tuan Ardouin August 11, 2020 06:20 PM UTC

Hello,

Is there a way to insert a conditional merge field inside the document editor ?

I'm already able to insert a merge field at the cursor position like so :

this.$refs.doceditcontainer.ej2Instances.documentEditor.editor.insertField('MERGEFIELD '+mfield+' \\* MERGEFORMAT', '{{'+mfield+'}}');

But I'm not able to reproduce your template when doing this :

this.$refs.doceditcontainer.ej2Instances.documentEditor.editor.insertField('MERGEFIELD { IF «company_1» == \"\" \"\" \" Data company 1 \\* MERGEFORMAT ');

Any suggestion ?

Thanks


HC Harini Chellappa Syncfusion Team August 12, 2020 04:58 PM UTC

Hi Tuan, 

Syncfusion Greetings! 

Currently, document editor doesn’t provide support to insert or edit the custom fields [barcode, date, if field, etc].  

We have logged this as feature request. It will be included in any of our upcoming releases. Will update the status of the feedback link once it is taken for implementation. Will update you once implemented. You can track the status from below link 


Regards, 

Harini C 



SK Sumanth K S April 20, 2023 10:05 AM UTC

Is there any update on this request?



SK Selvaprakash Karuppusamy Syncfusion Team April 21, 2023 02:43 PM UTC

Hi Sumanth,


As mentioned, we usually have an interval of at least three months between the releases. At the planning stage for every release cycle, we review all open features once again and finalize features for implementation based on specific parameters including product vision, technological feasibility, and customer interest. We will let you know when this feature is implemented.  


You can track the status of feature from the below link:  

https://www.syncfusion.com/feedback/8477/add-support-to-insert-and-edit-custom-fields 



Regards,

Selvaprakash K




KM Kavitha Muralitharan Syncfusion Team May 17, 2023 05:10 AM UTC


Sumanth, You can communicate with us or track the status of the feature using the below feedback link.

Feedback Link: https://www.syncfusion.com/feedback/8477/add-support-to-insert-and-edit-custom-fields 



Loader.
Live Chat Icon For mobile
Up arrow icon