We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

If condition in merge fields

I’m testing something similar and my fields seems to get populated but the IF code still display and the function never works.

My set up is this:
{ IF { MERGEFIELD ptyalias1 * MERGEFORMAT } <> “” { MERGEFIELD ptyalias1 * MERGEFORMAT }{ MERGEFIELD ptynm * MERGEFORMAT } * MERGEFORMAT }

If ptyalias and ptynm are populated this is what displays:
{ IF firstName <> “” firstName secondName * MERGEFORMAT }
If only ptynm us populated this is what displays:
{ IF <> “” secondName * MERGEFORMAT }

Is there a setting I need to have to get the IF function to work after the merge?

Could I get a copy of the test document you have to see if my formatting is wrong?

I’m testing something similar and my fields seems to get populated but the IF code still display and the function never works.

My set up is this:
{ IF { MERGEFIELD ptyalias1 * MERGEFORMAT } <> “” { MERGEFIELD ptyalias1 * MERGEFORMAT }{ MERGEFIELD ptynm * MERGEFORMAT } * MERGEFORMAT }

If ptyalias and ptynm are populated this is what displays:
{ IF firstName <> “” firstName secondName * MERGEFORMAT }
If only ptynm us populated this is what displays:
{ IF <> “” secondName * MERGEFORMAT }

Is there a setting I need to have to get the IF function to work after the merge?

Could I get a copy of the test document you have to see if my formatting is wrong?

I’m using this format because my original wasn’t working and I found the suggestion on an older post:

@beaustanko Could you please attach your template here for testing? We will check it and provide you more information.

Thank you for your reply. Here are my templates I am testing.

templates.7z (6.1 KB)

@beaustanko Thank you for additional information. As I can see conditions in your templates are evaluated properly. You can press Alt+F9 to switch between value and field code view in MS Word.
Also, you might noticed that by default Aspose.Words evaluates only fields of the displayed value in condition, i.e. if condition is true, mergefield in the true value is replaced with value and vice versa. You can control this behavior using MailMerge.UnconditionalMergeFieldsAndRegions flag.
If your goal is to have only the result value in the final document without field codes, you can use Document.UnlinkFields method. Or unlink field of the particular type only using Field.Unlink method.
For example the following code unlinks all fields in the document after executing mail merge:

Document doc = new Document(@"C:\Temp\IF-ELSE.doc");
doc.MailMerge.Execute(new string[] { "ptyalias1", "ptynm" }, new string[] { "", "false" });
doc.UnlinkFields();
doc.Save(@"C:\Temp\out.docx");

and the following code unlinks only IF fields:

Document doc = new Document(@"C:\Temp\IF-ELSE.doc");
doc.MailMerge.Execute(new string[] { "ptyalias1", "ptynm" }, new string[] { "", "false" });
foreach (Field f in doc.Range.Fields)
{
    if (f.Start.FieldType == FieldType.FieldIf)
        f.Unlink();
}
doc.Save(@"C:\Temp\out.docx");