Invalid tableIndex error after applying license

I have been working with the free version and the following code for inserting content into a table cell worked nicely:

var table = GetTableByTitle(title);
var allTables = _document.GetChildNodes(NodeType.Table, true);
var tableIndex = allTables.IndexOf(table);

var builder = new DocumentBuilder(_document);
builder.MoveToCell(tableIndex, currentRow, currentColumn, 0);

tableIndex would come back with value of 3.

After applying the trial license, this index changes to 7. I can still verify that it is the correct table, as it has the correct title associated with it, but now the MoveToCell call fails with:

Specified argument was out of the range of valid values.
Parameter name: tableIndex

This error happens even when the index changes.

@grinder22

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

Please find the project you requested attached. The word doc is in the project. All you need to do is compile and run. Comment out the license code at the top of Main and no exception is thrown.

thank you!

AsposeLicenseBug.zip (21.5 KB)

@grinder22

Thanks for your inquiry. Please note that DocumentBuilder.MoveToCell method moves the cursor to a table cell in the current Section. So, please get the tables of specific Section for which you want to insert content. Document.GetChildNodes returns the collection of nodes from whole document (header, footer and body).

Please use the following modified code to get the desired output.

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

var table = GetTable("Strips", doc);
var allTables = doc.Sections[0].Body.Tables;
var tableIndex = allTables.IndexOf(table);

var builder = new DocumentBuilder(doc);

try
{
    builder.MoveToCell(tableIndex, 0, 0, 0);
    Console.WriteLine("Success");
}
catch (Exception e)
{
    Console.WriteLine(e);
}

private static Table GetTable(string title, Document doc)
{
    var tables = GetTitledTables(doc);
    return tables.SingleOrDefault(t => t.Title == title);
}

private static IEnumerable<Table> GetTitledTables(Document doc)
{
    var tables = doc.Sections[0].GetChildNodes(NodeType.Table, true);
    foreach (Table table in tables)
    {
        if (!string.IsNullOrWhiteSpace(table.Title))
            yield return table;
    }
}
1 Like