Conditional Section

Howdy!

I wanted to see if it was possible to wrap a section of a document so that if I passed in a value that was null or false, it wouldn’t be displayed, using Mail Merge.

Edit: And also conditionally include pages.

Hi Richard,

Thanks for your inquiry. Yes, you can achieve your requirement using IF and Mail Merge fields. Please check the following example of IF and Mail Merge fields.

{ IF 1 = { MERGEFIELD Test } "True… some text" "False" }

I have attached the input and output documents with this post for your kind reference. Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "Test40.docx");
doc.MailMerge.Execute(new string[] { "Test" }, new string[] { "1" });
doc.Save(MyDir + " Out.docx");

Excellent, that did answer my question! I’ll be experimenting shortly, but at a glance I assume that means that formatted paragraphs and images may be in the merge fields “”?

Hi Richard,

Thanks for your inquiry.

Please note that Aspose.Words mimics the same behavior as MS Word does. Yes, the formatted paragraphs and images can be in IF field results as shown in Test40.docx. You can also replace mail merge fields with formatted text and images using Aspose.Words. Please use the DocumentBuilder.MoveToMergeField method to move the cursor to a position just beyond the specified merge field and insert contents according to your requirements.

Please let us know if you have any more queries.