简体   繁体   中英

Word found unreadable content error when opening file

I am facing an error " Word found unreadable content in abc.docx. Do you want to recover the content of this document? " while opening the Word (.docx) file.

I have tried with all the solutions given on the Internet but no success on this. Below is my code for writing the content into the stream.

private void test()
        {
            using (MemoryStream one = await db.DownloadFile("templates", "one.docx"))
            {
                using (MemoryStream two = await db.DownloadFile("templates", "two.docx"))
                {
                    using (MemoryStream newStream = new MemoryStream())
                    {
                        one.CopyTo(newStream);
                        editingMemoryStream.Position = 0;

                        using (WordprocessingDocument mainDoc = WordprocessingDocument.Open(one, true))
                        {
                            using (WordprocessingDocument newDoc =
                                WordprocessingDocument.Open(newStream, true))
                            {
                                Generate(modal, new, main, report);
                            }
                        }
                    }
                }
            }
        }
private void Generate(List<modal> mo, WordprocessingDocument new, MemoryStream report)
 {
     var main = new.MainDocumentPart;
     modal = mo[0];

     AddTableToBody(report, modal.table, mo);        
  }

public void AddTableToBody(MemoryStream temp, dailyReport,
                    MainDocumentPart main)
   {
            using (WordprocessingDocument newDoc = WordprocessingDocument.Open(editingMemoryStream, true))
            {
                WP.Body body= dailyReport.MainDocumentPart.Document.Body;
                var main = dailyReport.MainDocumentPart;
                //* some code is here*//

                var clone = dailyReport.CloneNode(true);
                main.Document.Body.AppendChild(new WP.Paragraph(new WP.Run(clone)));
                main.Document.Save();
            }
        }

The file contains 2 tables with some labels.

Looking at your code, it looks like you are adding a w:body element ( Body instance) to a w:r element ( Run instance) in the following two lines of code:

var clone = dailyReport.CloneNode(true);
main.Document.Body.AppendChild(new WP.Paragraph(new WP.Run(clone)));

In the above excerpt, clone is a Body instance ( w:body element) with all its child elements. I can only assume parent is the MainDocumentPart of some other WordprocessingDocument . You are appending a new Paragraph ( w:p ) with one w:r ( Run ) and one w:body ( Body ) with whatever children the latter has. This is invalid Open XML and most probably the reason why Word complains.

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