This project has moved and is read-only. For the latest updates, please go here.

Error when inserting HTML into Word table

Aug 8, 2014 at 7:27 PM
I may be doing something wrong as I am just learning the Open XML SDK, but when I embed the HTML into a table in the Word document and then open the Word document I get this error:

"The file ... cannot be opened because there are problems with the contents. Ambiguous cell mapping encountered. Possible missing paragraph element. <p> elements are required before every </tc>.

The table and HTML appear fine when Word recovers the contents, but I need to get rid of this error so users don't see it.

This is what that snippet of my code looks like:
tr = new TableRow();
tc = new TableCell();                                   
MainDocumentPart mainPart = wordDocument.MainDocumentPart;
HtmlConverter converter = new HtmlConverter(mainPart);
IList<OpenXmlCompositeElement> paragraphs;                                   
paragraphs = converter.Parse(htmlText);
for (int i = 0; i < paragraphs.Count; i++)
{
     tc.Append(paragraphs[i]);
}
tc.Append(new TableCellProperties(new TableCellMargin(
     new TopMargin { Type = TableWidthUnitValues.Dxa, Width = "72" },
     new LeftMargin { Type = TableWidthUnitValues.Dxa, Width = "72" },
     new RightMargin { Type = TableWidthUnitValues.Dxa, Width = "72" })));
                                  
tr.Append(tc);
table.Append(tr);
Coordinator
Nov 18, 2014 at 1:45 PM
Maybe a bit late... but adding a table is a bit complicated in OpenXml.
I suggest you to embed your Html code with "<table><tr><td>" + yourHtml + "</tr></td></table>"
and lets Html2OpenXml handle the table for you