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

Free Support Forum - aspose.com

Conditional Merge

Hi,

We’re wondering if it’s possible to do some conditional merge e.g., havine some part of a word template which should be dispoayed under certain data condition

Is this something possible ?

You should probably clarify by giving an example. The most basic way is done by Word itself.

Use an IF field. { IF {MERGEFIELD MyField} = "this" "show this" "show that" }

“show that” can be omitted, so that it either shows “show this” or nothing.

Hi Nicolas,

Thanks for your inquiry.

Jeff is absolutely correct. In addition to what Jeff has said, there are two possible techniques to insert conditional content into your document.

Firstly you can use an IF field in your document and insert the appropriate data into the conditional part using a mergefield. Please see the code below and Conditional Template 1.docx for an example:

Document doc = new Document("Conditional Template 1.docx");
doc.getMailMerge().execute(new String[] { "Type" }, new Object[] { "Radio" });
doc.save("Conditional Template Out 1.docx");

You can also use handler to provide your own logic when merging fields. In this case using Conditional Template 2.docx and the handler below, the appropriate message is merged into the document based off which value is passed to the mail merge engine.

Document doc2 = new Document("Conditional Template 2.docx");
doc2.getMailMerge().setFieldMergingCallback(new HandleFieldMerging());
doc2.getMailMerge().execute(new String[] { "Message" }, new Object[] { "Radio" });
doc2.save("Conditional Template Out 2.docx");
public static class HandleFieldMerging implements IFieldMergingCallback
{
    public void fieldMerging(FieldMergingArgs args) throws Exception
    {
        if(args.getFieldName().equals("Message"))
        {
            if(args.getFieldValue().equals("Radio"))
                args.setText("I am sorry you are having trouble with your radio");
        }
    }

    public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception
    {
        // Do Nothing
    }
}

Each method has it’s advantages, also note that the first method will leave the IF field in your document even after merging. You can use the code from here to convert such fields to static text.

If we can help you with anything else, please feel free to ask.

Best Regards,

Hi,

thanks for your answer.

I’m trying to make it work with error. I’m trying to do a if ield is not null, than show the value + , otherwise “”

I’ve tried this but it’sn not working…

{ IF {MERGEFIELD MyField} is not NULL {MERGEFIELD MyField}\r "" }

What should be the right syntax please ?

{ IF {MERGEFIELD MyField} <> “” "{MERGEFIELD MyField}
" }

Note these differences:

  1. Use of the <> for “not equals” and the pair of empty quotes for null.
  2. To the right of the empty quotes is another expression in quotes. This is the expression that is included in the output if the condition is true.
  3. Note that the ending quote of the true expression is on the 2nd line. Yes, this is exactly what you do. If you want a carriage return, you press return. If you want a tab, you press tab. If you want your result on the next page, you press ctrl/enter for the next page.

To go back to the basic example,

{ IF {MERGEFIELD myField} = "Jeff" "Hi, Jeff! What is your age?" "Hi. What is your name?" }

It works like this:

It checks to see whether myField = “Jeff”

If so, everything in the first quoted expression after that is included. Otherwise, everything in the second quoted expression is included.

If myField = Jeff, the output will be 3 lines, as follows:

Hi, Jeff!

What is your age?

If myField <> Jeff, the output will be only 1 line as follows:

Hi. What is your name?

Note: Always include a space before and after operators (the =, +, -, <> signs, etc.).

Also, your question is not really much of an “Aspose” question. It is more of a “Word” question. Here is a link to a reference to how to use various fields in Word:

http://office.microsoft.com/en-us/word-help/field-codes-in-word-HA102110133.aspx?CTT=1

Hi,

thanks for your answer.

I cannot merge any conditional, even as simple as

{ IF {MERGEFIELD FieldName} <> "" "HELLO WORLD"} }

The output document shows the conditional expression not resolved.

When I transform the text expression (using cntrl+F9), does it needs to be separate parts or, only one block ?

Thanks in advance for your answers.

I found all kinds of trouble when trying to use ctrl/f9 to create fields. I do not know why. After resorting to the menu (insert/quickparts/field), I have found it to be more stable.

What I do is I insert an empty IF field through the menu. After the IF field is inserted, I position my mouse to the right of the word “IF,” and I enter a MERGEFIELD field in the same way, except that in the menu I enter the name of the field (e.g., LastName).

Then, I go to the closing brace after the MergeField, and I press = and whatever condition I want. Then, I enter the 2 pairs of quotes for the true and false expressions. Then, I go to and put whatever I want inside the “true” quotes, etc.

If you are doing all of this testing inside Word, you can see the output by going to Print Preview. Alt/F9 only toggles the display of fields.

Hi JeffMatthews,

thanks a lot for your exemple, it work great now.

Thanks again.

Hi
Nicolas,

It is perfect that you managed to achieve what you’re looking for. Please let us know any time you have any further queries. We’re always glad to help you.

Best Regards,

Hi,

I cant quite understand you desccription above.

In my word document, if I insert a empty IF field , you get the message “Error! Missing test condition”

If you edit this and add a mergefield, it overides the IF field - I am obviously doing something wrong.

Thanks for any help

Ian.

Hi Ian,

Thanks for your inquiry. Could you please attach your problematic Word document (.doc/.rtf file) and code here for testing? I will investigate the issue on my side and provide you more information.

Best regards,

hi,

thanks for your quick reply.

the IF conditional is below the green heading.

the mergefield f_memberState is defined in a java class.

thanks
Ian.

Hi Ian,

Thanks for your inquiry and sorry for the delayed response. I did test your document with the latest version of Aspose.Words for Java (13.6.0) and was unable to reproduce any malfunction on my side. Please upgrade to the latest version of Aspose.Words from here and try run the following code snippet:

Document doc = new Document("C:\Temp\AmfReportTemplate.doc");
doc.getMailMerge().execute(new String[] { "f_memberState" }, new String[] { "BELGIUM" });
doc.save("C:\Temp\out.doc");

This resolves the IF field to “true” (please see the attached out.doc). However, specifying any other value to the MERGEFIELD will resolve the IF field to “false”. This is the expected behaviour. If we can help you with anything else, please feel free to ask.

Best regards,