Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Wed Jan 19, 2022 12:57 pm

Hi,

I am currently evaluating the Spire.Doc Java library to determine if it is suitable for my particular use case. The problem I am experiencing is with regards to mail merge conditional fields, I can't seem to get even the most basic of mail merge conditional fields to work. You can see the conditional field I am trying to evaluate below.

conditional field:
{ IF "{ MERGEFIELD Account.PaymentMethodType \* MERGEFORMAT }" <> "CreditCard" "Direct Debit" "Not Direct Debit" }

The above merge field evaluates to:
{ IF "Bank" <> "CreditCard" "Direct Debit" "Not Direct Debit" }

As can you see the simple field ({ MERGEFIELD Account.PaymentMethodType \* MERGEFORMAT }) evaluates fine but the conditional statement is not processed. I have included the code I am using as well as the template document for your convenience. Can you please assist me in this matter?

Version:
5.1.9

Template:
template.zip


Code:
Code: Select all
// open template
Document document = new Document();
document.loadFromFile("path-to-file", FileFormat.Docx);
      
// mock data
String[] fieldNames = new String[]{"Account.AccountNumber", "Account.PaymentMethodType", "BillToContact.Address"};
String[] fieldValues = new String[]{"C00000001", "Bank", "Address"};
      
// execute mail merge
document.getMailMerge().execute(fieldNames, fieldValues);
      
// save invoice
document.saveToFile("path-to-save-file", FileFormat.Docx);

cjericho
 
Posts: 17
Joined: Wed Jan 19, 2022 11:38 am

Thu Jan 20, 2022 6:17 am

Hello,

Thanks for your feedback.
I tested your case and the conditional statement executed fine. If the input value is "CreditCard", the result display value is "Not Direct Debit". If the input value is another value (including Bank), the result display value is "Direct Debit". Based on your description, I'm guessing that you are looking at the field code page when viewing the result Word file, not switching to the value page. If yes, please use the shortcut key "Alt+F9" to switch to the value page after opening the result Word file to view the field value, as shown in the screenshot.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1648
Joined: Wed Apr 07, 2021 2:50 am

Fri Jan 21, 2022 9:22 am

Hi,

You are right the shortcut key "Alt+F9" shows the correct field value. This was something I wasn't aware of as the rest of the fields just show the value upon opening the resultant Word file, will keep this in mind moving forward.

Thank you for your help!

cjericho
 
Posts: 17
Joined: Wed Jan 19, 2022 11:38 am

Fri Jan 21, 2022 9:57 am

Hello,

You are welcome.

If you encounter other issues related to our products in the future, please feel free to contact us.

Have a nice day!

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1648
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.Doc

cron