以编程方式修复 "Word found unreadable content in corrupt..."

Fixing "Word found unreadable content in corrupt..." programmatically

我正在从另一个系统获取 OpenXml 生成的 docx 文件。当尝试使用 Microsoft.Office.Interop.Word.Application.Open(filename) 在我的应用程序中打开文件时,出现 The file appears to be corrupted 异常。

当我手动打开 docx 文件时,我收到 Word found unreadable content in corrupt xxx.docx. Do you want to recover the contents of this document? If you trust the source of this document, click Yes. 提示。当我点击 Yes 时,它能够在一个新的未保存的 Word 文件中恢复文档。

我已经尝试将之前 corrupt.docx 文件的 document.xml 与 recovered.docx 文件的 document.xml 进行比较。虽然两个 document.xml 之间有许多格式更改(关闭 xml 标签之间有额外的 space),但主要区别在于 AltChunk 实际上嵌入到 recovered.docx 中并且有几个空的 "run" 标签被删除了。我不确定是什么原因导致文件被认为是损坏的,因为它们似乎不应该被认为是损坏的。

就是说,有没有办法 运行 当我通过我的应用程序以编程方式单击 Yes...Do you want to recover the contents of this document?... 提示时发生的任何过程;这将是理想的吗?不太好,有没有办法告诉 xml 的哪些部分实际上在 word 文档中损坏?

That said, is there a way to run whatever process happens when I click Yes to that ...Do you want to recover the contents of this document?... prompt programnatically through my application; this would be the ideal? Less preferably, is there a way to tell what parts of the xml is actually corrupting in a word doc?

  1. 不,那不暴露在外面
  2. 理论上,验证是可能的。但考虑到涉及 AltChunk,这可能不会出现问题。 AltChunk 的内容在 Word 处理文档之前不会集成,此时它会被集成。如果 "breaks" 中有什么,验证将不会接受它。

在这种特殊情况下,我可能会尝试手动删除 AltChunk(这些片段在 zip 文件中的几个地方)并查看文件是否可以在没有它的情况下打开。但是,如果您对 Word Open XML 压缩包不是很熟悉,最好询问文档的 producer/source。