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.
Unfortunately, activation email could not send to your email. Please try again.

IF Conditions in mailmerge template

Thread ID:

Created:

Updated:

Platform:

Replies:

121128 Nov 12,2015 11:51 AM May 1,2018 01:24 AM ASP.NET Web Forms 9
loading
Tags: DocIO
Christian Zeder
Asked On November 12, 2015 11:51 AM

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




Suganya Rathinam [Syncfusion]
Replied On November 13, 2015 02:12 AM

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


Christian Zeder
Replied On November 13, 2015 03:21 AM

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

Suganya Rathinam [Syncfusion]
Replied On November 15, 2015 11:56 PM

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


Christian Zeder
Replied On January 9, 2016 10:26 AM

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

Suganya Rathinam [Syncfusion]
Replied On January 11, 2016 05:56 AM

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


Christian Zeder
Replied On January 19, 2016 08:58 AM

Hi Suganya

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

Kind regards
Christian

Suganya Rathinam [Syncfusion]
Replied On January 19, 2016 11:28 PM

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


Tonya Foust
Replied On April 30, 2018 03:02 PM

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

Sethumanikkam Yogendran [Syncfusion]
Replied On May 1, 2018 01:24 AM

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


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.

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.

;