How to drop a section completely from a document

Hello,
Could you please provide a sample code to drop completely (delete full content) a section?
Request is to delete a section if it contains text “#DELETE_SECTION#”.
Input document and desired output document are attached.
Best regards, Evgeniy

Hi Evgeniy,

Thanks for your inquiry. Please use the following code snippet to be able to delete the whole Section that contains your specific filter text:

Document doc = new Document(@"C:\Temp\DropSection_input.doc");
foreach (Section sec in doc.Sections)
if (sec.Range.Text.Contains("#DELETE_SECTION#"))
   sec.Remove();
doc.Save(@"C:\Temp\out.doc");

I hope, this helps.

Best Regards,

Hi Awais,
Thank you very much for the solution! This works.
Best regards, Evgeniy

Hi Evgeniy,

Thanks for your feedback. Please let us know any time you have any further queries. We’re always glad to help you.

Best Regards,