Searching this forum I find many references to problems with using IF-conditionals and empty fields, with promisses of fixes, but it seems there are still a few basic problems. These problems prevent me from successfully using Aspose.Words in our production environment. The issues are:
When MS Word does a mail merge, it evaluates IF-conditionals and outputs the result in the merged document. Aspose on the other hand includes the IF-conditional in the merged document. This is bad for two reasons:
a) The merged documents cannot be sent directly to customers etc., since they contain other than plain text. I cannot explain to my customers they have to do Ctrl+A, F9, Alt-F9 before being able to print.
b) Because of the other issue listed below, using an IF-conditional to test for an empty field that can otherwise also include white space is impossible.
If a mergefield results in an empty string (within the IF-conditional), it is replaced by two double quotes (""). This results in an invalid IF-conditional if the entire mergefield was enclosed in double quotes. If a mergefield results in a string with a space, this is not enclosed in double quotes, which results in the infamous “invalid op code” error. The solution to the second is using double quotes around the mergefield, the solution to the second is not using double quotes. You see my dilemma.
The solution as I see it: Aspose should, like MS Word, evaluate IF-conditionals before producing the document, outputting a document without the conditionals being present. That will render the other issues moot. If this for some reason is really, really not possible, then fix the other issues, by always enclosing the result of the mergefield in an IF-conditional in double quotes.
– khs
We will consider adding an option or method to unlink fields after executing mail merge.
a) You do not need update field in the document after executing mail merge, even if there are IF statements. During executing mail merge Aspose.Words evaluates IF fields, so the actual value of the field should be shown in the final document (of course if option “Display Field Codes” is disabled in MS Word)
b) Could you please specify which version of Aspose.Words you use for testing? I tested with empty strings and all works fine on my side.
It might occur because you are using some old version of Aspose.Words. Anyways, please attach sample document and code, which will allow reproduce the problem.
We will consider adding an option or method to unlink fields after executing mail merge. a) You do not need update field in the document after executing mail merge, even if there are IF statements. During executing mail merge Aspose.Words evaluates IF fields, so the actual value of the field should be shown in the final document (of course if option “Display Field Codes” is disabled in MS Word)*
Well, as I wrote, the Aspose behaviour is different from that of Word, and it causes the problems I mention above.
b) Could you please specify which version of Aspose.Words you use for testing? I tested with empty strings and all works fine on my side.
According to our developers, it is version 8.0.0.0. When you are saying that it “all works fine” on your side, what behaviour do you say is “fine”? Do empty strings result in a pair of double quotes or not?
2. It might occur because you are using some old version of Aspose.Words. Anyways, please attach sample document and code, which will allow reproduce the problem.
As stated above, we use 8.0.0.0. Is it a known issue with this version? I see that version 9.2.0 is available, is that the most recent one?
– khs
With the latest version of Aspose.Words I see the correct behavior with double quotes, i.e. if it is necessary Aspose.Words insert double quotes, if it is not necessary Aspose.Words does not insert them. For example you have two IF field, like the following: { IF { MERGEFIELD test } = "test" "True text" "False text" } { IF "{ MERGEFIELD test }" = "test" "True text" "False text" }
If value of “test” mergefield is an empty string you will have the following output: { IF "" = "test" "True text" "False text" } { IF "" = "test" "True text" "False text" }
Yes, 9.2.0 is the most recent version. So, please try using it instead of old one.
alexey.noskov:
With the latest version of Aspose.Words I see the correct behavior with double quotes, i.e. if it is necessary Aspose.Words insert double quotes, if it is not necessary Aspose.Words does not insert them.
Thanks very much. I will ask our developers to use the latest version, so we can test with that one.
– khs
I have the same issue, after mail merge the doc files shows the merge fields and I have to press ALT-F9 to hide the IF field codes. Any suggestions ? I am using version 13.2.0.0.
Thanks for your inquiry. I think, you should instruct the mail merge engine to remove any containing fields from around a merge field during mail merge by using the MailMergeCleanupOptions.RemoveContainingFields property. If the MailMergeCleanupOptions.RemoveContainingFields flag is enabled then not only would the merge field be removed, but also the outer IF field is removed as well leaving just the plain text result of the IF field.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.