If condition in merge field

I have been facing a little problem in adding “IF” condition in a merge field.
I have this kind of situation in my template
<<field1>><<field2>>
What my requirement is that if value of field1 is not empty then field 2 should be shown.
Kindly propose me a solution to solve this.
Shahid

This message was posted using Aspose.Live 2 Forum

Hi

Thanks for your request. Please see the attached template and the following simple code:

Document doc = new Document("in.docx");
doc.MailMerge.Execute(new string[]
{
    "Field1",
    "Field2"
}, new string[]
{
    "",
    "TestField2"
});
doc.Save("out.docx");

Please let me know in case of any issues I will be glad to help you.
Best regards,

I have an API that does the mail merge for multiple different templates. So i cannot change the mail merge code logic.
Is there any way that i could handle it in my template?

Hi

Thanks you for additional information. You can use your own mail merge API. In my example I try showing you how to use IF conditions (please see my document one more time). You will see the following IF construction:
{ IF {MERGEFIELD Field1 \* MERGEFORMAT} = "" {MERGEFIELD Field2 \* MERGEFORMAT } {MERGEFIELD Field1 \* MERGEFORMAT} \* MERGEFORMAT }
After mail merge you will get needed result.
Best regards,

Thanks. That issue have been fixed but have another issue now.
How can i hide a section? Let say i have a field in my input XML say “ShowContract”. If value of this field is “true” then region “contract” should be shown and if value of it is false, region "contract should be invisible.
How can i put an if condition over a region?

Hi

Thanks for your request. In this case you should use the same technique. Please see the attached template and the following code:

Document doc = new Document("Template.docx");
doc.MailMerge.Execute(new string[]
{
    "ShowContract"
}, new string[]
{
    "true"
});
doc.Save("out.docx");

Best regards,

That is a possible solution but i have some other issues.
I have one service that does mail merge for my dozen of applications. I cannot add this kind of thing in my code. Is there any way, i could handle it on template?
Possible solution would be a region under a “IF” condition.

Hi
Thanks you for additional information. As I mentioned you earlier, you can use your own mail merge API. Please see the attached document from my previous post. You can put all what you need inside IF condition. Also please see MS Word documentation.
Best regards,

A post was split to a new topic: If condition in merge fields