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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Conditional sections (if/else) in mail merge

Thread ID:

Created:

Updated:

Platform:

Replies:

156835 Sep 9,2019 11:18 AM UTC Aug 12,2020 04:58 PM UTC ASP.NET MVC - EJ 2 5
loading
Tags: DocumentEditor
Tomasz Jagusz
Asked On September 9, 2019 11:18 AM UTC

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.

Poorani Rajendran [Syncfusion]
Replied On 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


Tomasz Jagusz
Replied On 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

Poorani Rajendran [Syncfusion]
Replied On 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.


Tuan Ardouin
Replied On 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

Harini Chellappa [Syncfusion]
Replied On 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 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon