Hi,
I’m using Aspose words-net mailmerge and I am trying to show a MergeField with an IF field when another MergeField is empty like this:
{ IF { MERGEFIELD DepartureDate \* MERGEFORMAT } = "" "{ MERGEFIELD DepartureDateConfirmation \* MERGEFORMAT }" \* MERGEFORMAT }
I expected the field “DepartureDateConfirmation” to be shown when “DepartureDate” is empty.
It prints the unmerged version of the field: << DepartureDateConfirmation >>
instead of the expected content.
without the IF Field the mergeFiedl is successfully replaced
also hiding static text with the IF works
but not the combination of both.
Is this expected behaviour?
How can I show/hide text based on other mergefields?
@kevin.parret You should put the merge field in the condition into quotes:
{ IF "{ MERGEFIELD DepartureDate \* MERGEFORMAT }" = "" "{ MERGEFIELD DepartureDateConfirmation \* MERGEFORMAT }" "Some false value" }
Also, you can specify MailMergeCleanupOptions.RemoveContainingFields so after execution mail merge only the calculated value of IF field is shown.
@kevin.parret Here is a simple template with IF field like yours: in.docx (12.4 KB)
After executing mail merge using simple code:
Document doc = new Document(@"C:\Temp\in.docx");
doc.MailMerge.Execute(new string[] { "DepartureDate", "DepartureDateConfirmation" }, new string[] { "", "DepartureDate Is Empty" });
doc.Save(@"C:\Temp\out.docx");
The result shows the value of DepartureDateConfirmation
field: out.docx (9.7 KB)
@alexey.noskov, thanks for the help!
turns out my IF statemant was indeed correct (which I could validate by your statement).
The actual problem was, the previous developer thought it was a good idea to call MailMerge.Execute multiple times.
The fields DepartureDate and DepartureDateConfirmation where in different executions and in the wrong order. I fixed it by only exectuting the MailMerge once with all data.
thanks again for the help!
@kevin.parret It is perfect that you managed to find and resolve the problem. Please feel free to ask in case of any issue. We are always glad to help you.