Conditional format (text color) in Mail Merge
Hi there,
I really like how Mail Merge works,
but I have one problem I struggling with.
I have a field, let's say "CorrectAnswer" and it contains values "Yes" or "No".
I can easily display its value in Word using any sample form docs:https://help.syncfusion.com/file-formats/docio/working-with-mail-merge
My problem is with conditional formatting - how can I display "Yes" in green and "No" in red?
Is this even doable?
If yes then I'll be more than grateful for a sample.
SIGN IN To post a reply.
3 Replies
MR
Manikandan Ravichandran
Syncfusion Team
March 31, 2020 05:25 PM UTC
Hi Tomasz,
Thank you contacting Syncfusion support.
Yes, you can achieve your requirement in two ways. First way is in document level and another one is in code level.
Suggestion – 1:
you can create the template document with merge field along with IF fields. Please refer the screenshot and document as follows.
After performing mail merge, you should use “UpdateDocumentFields” API to update the fields in the document.
Please find the template document with merge field with IF field. https://www.syncfusion.com/downloads/support/forum/152852/doc/Template728627994
Suggestion – 2:
You can use the following code example to achieve your requirement in code level.
Thank you contacting Syncfusion support.
Yes, you can achieve your requirement in two ways. First way is in document level and another one is in code level.
Suggestion – 1:
you can create the template document with merge field along with IF fields. Please refer the screenshot and document as follows.
After performing mail merge, you should use “UpdateDocumentFields” API to update the fields in the document.
Please find the template document with merge field with IF field. https://www.syncfusion.com/downloads/support/forum/152852/doc/Template728627994
Suggestion – 2:
You can use the following code example to achieve your requirement in code level.
|
WordDocument document = new WordDocument(@"Template1.docx"); string[] fieldNames = new string[] { "CorrectAnswer" }; string[] fieldValues = new string[] { "Yes" }; //Uses the mail merge events to perform the conditional formatting during runtime document.MailMerge.MergeField += new MergeFieldEventHandler(ApplyTextColor); //Performs the mail merge document.MailMerge.Execute(fieldNames, fieldValues); document.Save("Output.docx"); document.Close(); |
|
private static void ApplyTextColor(object sender, MergeFieldEventArgs args) { //Sets text color to the alternate mail merge record if (args.FieldName == "CorrectAnswer" && args.FieldValue == "Yes") { args.TextRange.CharacterFormat.TextColor = Color.Green; } else if(args.FieldName == "CorrectAnswer" && args.FieldValue == "No") { args.TextRange.CharacterFormat.TextColor = Color.Red; } } |
Please find the template document with merge field alone.
https://www.syncfusion.com/downloads/support/forum/152852/doc/Template11493369690
Please refer our UG documentation to know more about mail merge events.
https://help.syncfusion.com/file-formats/docio/mail-merge/mail-merge-events
Please let us know if you have any other questions.
Regards,
Manikandan Ravichandran
TJ
Tomasz Jagusz
April 2, 2020 02:16 PM UTC
Hi Manikandan,
thank you for both solutions.
I will try them both.
First one is more complicated - it requires adding IF field in word, but I also think it is better :)
Best regards,
Tomasz
MR
Manikandan Ravichandran
Syncfusion Team
April 3, 2020 09:19 AM UTC
Hi Tomasz,
Thank you for your update.
Please check both suggestions and reverts us your feedback for this.
Please let me know if you have any concern on this
Regards,
Manikandan Ravichandran
Thank you for your update.
Please check both suggestions and reverts us your feedback for this.
Please let me know if you have any concern on this
Regards,
Manikandan Ravichandran
SIGN IN To post a reply.
- 3 Replies
- 2 Participants
-
TJ Tomasz Jagusz
- Mar 30, 2020 03:09 PM UTC
- Apr 3, 2020 09:19 AM UTC