1
votes

I'm trying to create/manipulate Word .docx files using the DocX nuget package.

In the documentation they provide the following example:

// Place holder for a Table.
Table t;

// Load document a.
using (DocX documentA = DocX.Load(@"C:\Example\a.docx"))
{
    // Get the first Table from this document.
    t = documentA.Tables[0];
}

// Load document b.
using (DocX documentB = DocX.Load(@"C:\Example\b.docx"))
{
    /* 
     * Insert the Table that was extracted from document a, into document b. 
     * This creates a new Table that is now associated with document b.
     */
    Table newTable = documentB.InsertTable(t);

    // Save all changes made to document b.
    documentB.Save();
}// Release this document from memory.

When the code executes I receive an error when inserting the table: Table newTable = documentB.InsertTable(t);

Error: System.InvalidOperationException {"Sequence contains no elements"}

I am at a loss as to why this is occurring. I have looked at the table "t" that is being inserted and it appears to have all of the properties populated. I am unclear as to what is causing the error.

Any help would be greatly appreciated.

1

1 Answers

1
votes

I have switched over to using the lastest version of the DLL downloaded from docx.codeplex.com.

I was still having this problem. However I then compared the table that I was coping from my source document to a table that I created directly using the DocX.dll.

I found that for some reason the Design property on my source table was not set to anything, however the table that I created directly was set to Novacode.TableDesign.TableGrid.

I am now setting this property on my source table and everything works as expected.

mytable.Design = Novacode.TableDesign.TableGrid;

I suspect this is a problem in my source document that I am modifying. However since I am not the original author I have no control over this.

Manually setting this property is allowing me to clone the source table, and my solution is now working.