简体   繁体   中英

Why do I get 'Word found unreadable content in .doc' when adding a nested table

I am using OpenXML to build a word document with a nested table, ie a table within a table. However, adding the nested table results in a 'Word found unreadable content in.doc' pop-up message when trying to open the file in Word for MS 365.

Removing the nested table, or replacing it with text, results in a file that can be opened without issue.

Recovering the file in Word for MS 365 by clicking 'Ok' on the pop up which displays 'Do you want to recover the contents of this document?' results in a file that can be opened and looks correct, but that isn't a viable solution.

I've looked at the XML generated by the recovered version of the file, but quite a lot of changes (files like settings.xml and webSettings.xml are added, for example) that don't appear directly relevant to having a table within a table.

Here is the code used to generate the MemoryStream of the doc with a table within a table:

 MemoryStream mem = new MemoryStream();
           
// Create Document
using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(mem, WordprocessingDocumentType.Document, true))
{
    // Add a main document part. 
    MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();

    // Create the document structure and add some text.
    mainPart.Document = new Document();
    mainPart.Document.Body = new Body();
    Body docBody = mainPart.Document.Body;

    Table formTable = new Table();

    string sectionName = "Section Name like Core Information";
    FontSize fontSize = new FontSize();
    fontSize.Val = "30";

    RunProperties runProp = new RunProperties(fontSize);
    Run tableRun = new Run(runProp);
    tableRun.AppendChild(new Text(sectionName));
    TableCell tableCell = new TableCell(new Paragraph(tableRun));

    TableCellProperties tblCellProperties = new TableCellProperties(new TableCellWidth() { Width = "5000", Type = TableWidthUnitValues.Pct });
    tableCell.Append(tblCellProperties);

    TableRow sectionRow = new TableRow(tableCell);


    TableCell alternativeCell = new TableCell();
    Table subTable = new Table();
    Run subtableRun = new Run(runProp.CloneNode(true));
    subtableRun.AppendChild(new Text("Sub Table Run"));
    TableCell subTableCell = new TableCell(new Paragraph(subtableRun));
    TableCellProperties subTblCellProperties = new TableCellProperties(new TableCellWidth() { Width = "5000", Type = TableWidthUnitValues.Pct });
    subTableCell.Append(subTblCellProperties);
    TableRow subTableRow = new TableRow(subTableCell);
    subTable.AppendChild(subTableRow);
    alternativeCell.Append(subTable);
    TableRow RowHousingSubTable = new TableRow(alternativeCell);
    // make a subform i.e. sub table here

    formTable.AppendChild(sectionRow);
    formTable.AppendChild(RowHousingSubTable);

    docBody.Append(formTable);
}
return mem;

I then save the MemoryStream to disk with

var bytes = stream.ToArray();
File.WriteAllBytes(@"C:\temp\word_crash.docx", bytes);

Adding an empty paragraph after the nested table will fix this, ie

 alternativeCell.Append(subTable);
 alternativeCell.Append(new Paragraph()); // add this line

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM