I’m using OpenXML SDK with Word to produce labels. The base Word doc has one page with a table, two rows and two cells where each cell represents one label. My plan was to add pages as needed based on the size of a SQL result set. The append of the page works but the count of tables in the Body doesn’t change. Is there a way to refresh the Element array? I suppose I can get the size of the result set and append the required number of pages then close and reopen the doc but I’m guessing I’m missing something simple.
Reported the wrong problem sorry, the doc looked correct in Word but not in the productivity tool. I updated the code to show my latest attempt. iTest now reports the correct number of tables but in the productivity tool the added tables are flagged as OpenXmlUnknownElement.
for (int i = 0; i < pages; ++i)
{
Table table = new Table(doc.MainDocumentPart.Document.Body.Elements<Table>().First().CloneNode(true));
doc.MainDocumentPart.Document.Body.Append(table);
}
int iTest = doc.MainDocumentPart.Document.Body.Elements<Table>().Count();
the xml shows there is an extra <w:tbl> generated:
<w:tbl xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:tbl>
<w:tblPr>
<w:tblStyle w:val="TableGrid" />
So the real question is, how to copy and paste a table with OpenXML SDK?
new and CloneNode were redundant, also InsertAfter wasn’t necessary but kept the tables together. Since I’m not updating the labels until later inserting after First() works for my purpose.