We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Iterate over all tables in word document?

I am creating a new Aspose word document based on an existing word document template.

The template I have has 7 tables in the document.

I want to iterate over all of the tables in the document.

I have tried grabbing the table collection like this doc.FirstSection.Body.Tables, however this only returns 3 tables.

If I look at this line of code: doc.Sections.Count it returns 2
So then if I look at these lines of code:
doc.Sections[0].Body.Tables.Count it returns 3
doc.Sections[1].Body.Tables.Count it returns 4

So apparently my document has 2 sections, nice. I don’t care if it has 2 sections or 2 billion, is there not an easy way to iterate over ALL of the tables in the document something like:
doc.Body.Tables which would return all tables in the document ?

Or I’m open to suggestions as to how to accomplish this.


Hi David,

Thanks for your inquiry. Please use CompositeNode.GetChildNodes method (NodeType, Boolean) to get a live collection of child nodes that match the specified type.

A live collection is always in sync with the document. For example, if you selected all sections in a document and enumerate through the collection deleting the sections, the section is removed from the collection immediately when it is removed from the document.

Please check the following code example to achieve your requirements and let us know if you have any more queries.

Document doc = new Document(MyDir + "in.docx");

foreach (Table tabl in doc.GetChildNodes(NodeType.Table, true))