Hi guys,
I stucked in a Scenario. I have a template file(.dotx) which has multiple tables in it.
I’m able to fetch the very 1st table and to manipulate required data using the following code:
Table table = doc.FirstSection.Body.Tables[0];
But I can’t get the other tables using doc.FirstSection.Body.Tables[1] or doc.FirstSection.Body.Tables[2].
Can anybody please help me out ?
Let me tell you guys one more thing that the tables are in different pages of the template file.
Is that the reason I’m getting this error:
System.NullReferenceException: ‘Object reference not set to an instance of an object.’
table was null.
@suprateembose doc.FirstSection.Body.Tables
returns only table from the first section of the document. MS Word document can contain any number of section. Please see our documentation to learn more about Aspose.Words Document Object Model. In your case if you need to get all tables in the document, you can use GetChildNodes method:
NodeCollection tables = doc.GetChildNodes(NodeType.Table, true);
@alexey.noskov Hey thanks for quick reply. I just want to ask that your suggestion will return all the tables collection from the document right ? So from that collection can we take one particular table ? or I need to cast it to (Aspose.Words.Table) this datatype?
Actually I wanted to work on that particular table, with its rows and columns.
@suprateembose GetChildNodes
method returns NodeCollection
, an instance of Node
class is returned from this collection by index. So to get a particular table from the collection you should cast it:
NodeCollection tables = doc.GetChildNodes(NodeType.Table, true);
Table t = (Table)tables[0];
Also, in your case you can use GetChild method, it returns a single node of the specified type:
Table t = (Table)doc.GetChild(NodeType.Table, 0, true);