Free Support Forum - aspose.com

Conditional Merge Text

I understand that IF statements are not supported in MergeFields at the moment and are ignored during the Merge process.

I also understand that you can use the Document Mailmerge MergeHandler event for postprocessing of formatting on Merge Fields.

I need to change the value of a merge field based on a list of options at merge time. The same mergefield in different locations may be changed into any one of the options. These options that can only be held in the document field itself as they are per each instance of the mergefield. I want to do this by adding custom flags to the mergfield and handling them in the MergeHandler Event.

For example the field may be ClientType which chould be one of (Male, Female or JointParty). ClientType may be present in several palces in the document. In the first location I need it to display He if a Male, She if a Female or They if JointParty.

He requires a new Contract

She requires a new Contract

They require a new Contract

The bolded text shows where the mergefield has been inserted and translated

Elsewhere in the document it may say

His Claim

Her Claim

There claim

These merges are based on the value of ClientType which can equal (Male, Female or JointParty)

I can catch the merge event for this field in the MergeHandler Event handler, I also know that if I have data tagged to the mergefield I can convert the values to the ones I want.

This is a feature that I must be able to implement inorder to use Aspose.Words in this project - is this possible or is there a better way to accomplish what I am trying to do?

Cheers Bruce

There is no 'one-liner' for doing it. You need to implement this logic in application code. I have composed a small example for you to start with. The test document has the following contents:

«PersonalPronoun» require«VerbEnding» a new Contract.

«PosessivePronoun» claim is approved.

«PersonalPronoun» ask«VerbEnding» for a new option.

«PosessivePronoun» suggestion is confirmed.

After execution of the following code:

string filename = Application.StartupPath + @"\testConditionalMerge.doc";

Document doc = new Document(filename);

string clientType = "He";

string[] fieldNames = new string[] {"PersonalPronoun", "PosessivePronoun", "VerbEnding"};

object[] fieldValues;

switch (clientType)

{

case "They":

fieldValues = new object[] {"They", "their", ""};

break;

case "She":

fieldValues = new object[] {"She", "her", "s"};

break;

default:

fieldValues = new object[] {"He", "His", "s"};

break;

}

doc.MailMerge.Execute(fieldNames, fieldValues);

doc.Save(MakeModifiedName(filename));

You will get the following result:

He requires a new Contract.

His claim is approved.

He asks for a new option.

His suggestion is confirmed.

hi Thanks for the reply

unfortuanatly this will not work where the mail merge field is in more than one place in the document.

For example in the first location of the mergefield ClientType needs to be translated from "Joint Party" to They ie( They require a new contract) in the second location of the mergefield ClientType needs to be translated from "JointParty" to Theirs (the property will become theirs). The MergeField data from the datasource in this case is constant ("JointParty") but needs to be translated into different values based on a flag contained in the individual merge field.

I have een able to do this with XSLT - the mergefield looks like .

Given that ClientType="JointParty" then "Theirs" is inserted

when it is translated it runs application logic to determine which choice has been selected and inserts the relevant MergeText value into the document

However the next occurance requires a different value merged to the document

Given that ClientType="JointParty" then "Them" is inserted

Keep in mind that this is the same MergeField in multiple places in the same document

Cheers Bruce

If you have any suggestions on how to represent this kind of functionality in Aspose.Words API we will consider it for implementing in our next version.