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

IF Conditions in mailmerge template

Hi all,

Is it possible to use IF conditions in DocIo? If i try it with usual word syntax, it doesn't work. The result is always "Female" because the mergefield is not updated correctly.

{ IF { MERGEFIELD Gender } = "M" "Male" "Female"  }

How can I do this? Thanks for your help.

Kind regards
Christian




13 Replies

SR Suganya Rathinam Syncfusion Team November 13, 2015 07:12 AM UTC

Hi Christian,

Thank you for contacting Syncfusion support.

On further analysis of your requirement, we found that you are using Merge field in the If field expression. Currently DocIO doesn’t process the Merge fields within the fieldcode of If fields. We have prepared a sample for your requirement. Please find the sample from the following link and let us know if it helps.

Sample Link:
http://www.syncfusion.com/downloads/support/forum/121128/ze/Sample1562194760.zip

Please let us know if you have any other questions.

Regards,
Suganya



CZ Christian Zeder November 13, 2015 08:21 AM UTC

Hi Suganya

I'm not sure if I understand you correctly. It's not possible to use an existing word template with IF fields within a mail merge run? I've to create the IF expression in code?

WField field = paragraph.AppendField("If"FieldType.FieldIf) as WField;
field.FieldCode = "IF ";
int fieldIndex = paragraph.Items.IndexOf(field);
WMergeField mergeField = new WMergeField(document);
mergeField.FieldCode = "MERGEFIELD Gender";
paragraph.Items.Insert(fieldIndex + 1, mergeField);
WTextRange text = new WTextRange(document);
text.Text = " = \"M\" \"Male\" \"Female\"";
paragraph.Items.Insert(fieldIndex + 2, text);            
string[] fieldName = { "Gender" };
string[] fieldValue = { "F" };
document.MailMerge.Execute(fieldName, fieldValue);
document.UpdateDocumentFields();
document.Save("Sample.docx"FormatType.Docx);
Kind regards
Christian


SR Suganya Rathinam Syncfusion Team November 16, 2015 04:56 AM UTC

Hi Christian,

Thank you for your update.

Mail merge can be executed for Merge fields within the fieldcodes of IF fields using DocIO. We have modified the sample for your requirement. Please find the modified sample from the following link and let us know if it helps.

Sample Link:
http://www.syncfusion.com/downloads/support/forum/121128/ze/Sample-1936984544.zip

Please let us know if you have any other questions.

Regards,
Suganya



CZ Christian Zeder January 9, 2016 03:26 PM UTC

Hi Suganya

Thanks for your sample. It works, but i think it's not sufficient for my case. Perhaps i'm wrong, but i'll try to explain it in more detail. I've declared one or many IF statements in a word template as in your sample. In this case "Gender" or "AddressField" is a columnname from my datasource.

{ IF { MERGEFIELD Gender } = "M" "Male" "Female"  }
{ IF { MERGEFIELD AddressField } = "" "No Address" AddressField  }

As a next step i try to generate mailmerge documents with this code, it's not specific for a template. 

// create a document instance, based on a template (path is only known at runtime)
var document = new WordDocument();
document.Open(_templatepath);   

// generate mailmerge document, use datatable for all mergefields 
// (datatable columns are only known at runtime, for this sample Gender, AddressFeld, etc.)
document.MailMerge.Execute(_dataTable);

// update if statements (but i don't work as expected)?!
document.UpdateDocumentFields();

//create memory stream 
document.Save(ms, FormatType.Word2013);

// At the end create a PDF 
PdfDocument pdfDocument = converter.ConvertToPDF(ms);
using (var pdfMs = new MemoryStream())
{
     pdfDocument.Save(pdfMs);
}

Possible? Thanks for you help.

Kind regards
Christian


SR Suganya Rathinam Syncfusion Team January 11, 2016 10:56 AM UTC

Hi Christian,

Thank you for your update.

We are not able to reproduce the mentioned field not updated issue using DocIO and we have prepared a sample to reproduce the issue at our side. Please find the sample we have tried at our end from the following link. Kindly modify the sample to reproduce the issue at our end.

Sample Link:
http://www.syncfusion.com/downloads/support/forum/121128/ze/Sample220860592.zip

If we misunderstood any of your requirement then kindly elaborate the actual requirement with detailed description and also your product version. Thereby we will analyze further on updating the document fields and will provided you appropriate solution.

Please let us know if you have any questions.

Regards,
Suganya



CZ Christian Zeder January 19, 2016 01:58 PM UTC

Hi Suganya

With current version of DocIO (13.3400.0.7) it works as expected. Thanks a lot for your help.

Kind regards
Christian


SR Suganya Rathinam Syncfusion Team January 20, 2016 04:28 AM UTC

Hi Christian,

We are happy to hear that the issue has been resolved in 13.3.0.7 version.
Please let us know if you need further assistance. We will be happy to assist you as always.

Regards,
Suganya



TF Tonya Foust April 30, 2018 07:02 PM UTC

Good Afternoon.

I am experiencing a similar issue. I am working on a WinForms Desktop application.
I am trying to create a document using a Word 2016 DOTX file. The Merge Fields are being replaced as expected, but the IF conditions are not being resolved.

My template has conditions such as:
{ IF "{MERGEFILED IsPierceFirm }"= "True" "Do this" "Else do that" }
And it resolved to:
{ IF "True"= "True" "Do this" "Else do that" }

My code loops through a data table to build an array which is then passed to the Execute method:
            document.MailMerge.Execute(fieldNames, fieldValues);
            document.UpdateDocumentFields();
            document.MailMerge.RemoveEmptyParagraphs = true;
            document.MailMerge.ClearFields = true;

I am using version 16.1451.0.24 (runtime version 4.0.30319).
I just cannot see what I am doing wrong. Any suggestions would be greatly appreciated.
Thank you,
Tonya Foust


SY Sethumanikkam Yogendran Syncfusion Team May 1, 2018 05:24 AM UTC

Hi Tonya,

Thank you for your update.

We suspect that the reported IF field preservation issue might occurred due to the contents (elements) representation in your input template or code snippet with other data in different scenario based on the input data for Mail merge used at your end.

We need to analyze with your input Word document and Mail merge data to validate the mentioned problem at our end. So, kindly provide us the following details which will be helpful to analyze further on your requirement and update you the appropriate solution at the earliest.

     1. Code snippet or simple runnable sample.
     2. Input template.
     3. Data set (dummy) used for Mail merge.

Note: If you have any confidential data in your Word document, please replace with some dummy data and provide us the same.

Please let us know if you have any other questions.

Regards,
Sethumanikkam.Y



HM HARIKRISHNAN MP replied to Suganya Rathinam October 19, 2022 10:48 PM UTC

Hi Suganya, 

The aforementioned code snippet (https://www.syncfusion.com/forums/121128/if-conditions-in-mailmerge-template?reply=lEp3NS ) is not working with DocIO v20.2. 

Can you please verify and send a sample compatible with the latest version. 


Regards

Hari 



AN Anto Nihil Sahaya Raj Syncfusion Team October 20, 2022 02:07 PM UTC

Hi Hari,

Essential DocIO library process the merge fields within the field code of If fields.

From v16.1.0.24, we have migrated the field to the new structure. So, you have faced the problem in that old sample code example which has the old field structure.

Please refer the below link to know more about this migration,
https://help.syncfusion.com/file-formats/release-notes/migratingtov16.1.0.24


We have prepared the sample application in the latest version to process the merge fields within the field code of If fields as per your requirement and it can be downloaded from the below link.
https://www.syncfusion.com/downloads/support/directtrac/general/ze/Sample-2029884514

Please refer our UG documentation link to know more about mail merge in the Word document.
https://help.syncfusion.com/file-formats/docio/working-with-mail-merge

Regards,
Anto Nihil S

If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.



HM HARIKRISHNAN MP replied to Anto Nihil Sahaya Raj October 21, 2022 06:29 AM UTC

Hi Anto, 

Thanks for your input. The sample works perfect. 

Further to your sample, If I may ask - what can be done to include merge fields as a true or false statement in the IF field. 

i.e. { IF 1=1 <<MergeField>> False } - I want to show the true expression as a merge field value. 


Regards, 

Hari 



SB Suriya Balamurugan Syncfusion Team October 24, 2022 07:13 AM UTC

Hi Hari,

From the given details, we have found that your requirement is to include merge fields as a true or false statement in the IF field.

We have prepared the sample application to show the true expression as a merge field value as per your requirement and it can be downloaded from the below attachment.

Regards,
Suriya Balamurugan.


Attachment: MergeField_In_True_Expression_ad6509a2.zip

Loader.
Up arrow icon