In the following code running the attached files, when I try and insert a table row I get the error: “'Object reference not set to an instance of an object.”
I am inserting the Error_RowTest file into Error_TableTest and Error_Output shows the expected output. What I am trying to do is find a table by title, add a new row to that table and insert the information from the row file into the table.
ERROR_RowTest.zip (79.5 KB)
This is my code…
InsertTableRow(@“c:\temp\error_rowtest.docx”, @“c:\temp\error_tabletest.docx”, “Strawberry Delight”, @“c:\temp\Error_Output.docx”);
private static void InsertTableRow(string rowFilenameAndPath, string tableFilenameAndPath, string tableTitle, string outputFilenameAndPath)
{
Document tableDoc = new Document(tableFilenameAndPath);
Document rowDoc = new Document(rowFilenameAndPath);
NodeCollection tables = tableDoc.GetChildNodes(NodeType.Table, true);
foreach (Aspose.Words.Tables.Table table in tables)
{
if (table.Title == tableTitle)
{
NodeImporter imp = new NodeImporter(rowDoc, tableDoc, ImportFormatMode.KeepSourceFormatting);
Node impNode = imp.ImportNode(rowDoc.FirstSection.Body.Tables[0].Rows[0], true);
tableDoc.FirstSection.Body.Tables[1].Rows.Add(impNode);
break;
}
}
tableDoc.Save(outputFilenameAndPath);
}