Free Support Forum - aspose.com

How will I get the Merge Fields collection for a particular section in a document

Hey Aspose Support,

I am using C# code.

How will I get the Merge Fields collection for a particular section in a document ? Please give a code example…

I want to check whether the section contains no merge fields. If yes, then.delete that section in the document. How will I be able to do this.? Please provide a code example…

Thanks…

Hi Sanjay,


Thanks for your inquiry. Please use the following code snippet to achieve your requirements. Hope this helps you. Please let us know if you have any more queries.

<span style=“font-size:
10.0pt;font-family:“Courier New”;color:#2B91AF;mso-no-proof:yes”>ArrayList<span style=“font-size:10.0pt;font-family:“Courier New”;mso-no-proof:yes”>
sectionList = new ArrayList();<o:p></o:p>

Document doc = new Document(MyDir + "MailMergeSection.doc");

foreach (Section section in doc.Sections)

{

//Get all field of a specific section

foreach (FieldStart fStart in section.GetChildNodes(NodeType.FieldStart, true))

{

//check filed type

if (fStart.FieldType == FieldType.FieldMergeField)

{

sectionList.Add(section);

break;

}

}

}

foreach (Section section in sectionList)

{

section.Remove();

}

doc.Save(MyDir + "out.doc");