如何以编程方式判断word文档是否已损坏? [英] How to programatically tell if a word document is corrupt?

查看:84
本文介绍了如何以编程方式判断word文档是否已损坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小 C# 应用程序,它可以与 word 互操作,将一堆 word .doc 文件转换为文本文件,并且在大多数情况下都可以正常工作.

I've got a little C# application that interops with word converting a bunch of word .doc files into textfiles and for the most part this works fine.

但是,如果文档已损坏,则 word 无法打开该文件并弹出一个对话框,这意味着我无法完全自动执行此转换过程 - 必须有人注意对话框.

However, if the document is currupt then word cannot open the file and a dialog box pops up, which means that I cannot fully automate this conversion process - someone has to watch for the dialogs.

有没有办法在不打开它的情况下测试一个单词 .doc 是否已损坏?也许通过文字互操作,或者通过第 3 方工具.

Is there a way to test if a word .doc is currupt, without opening it? Perhaps through word interop or maybe through a 3rd party tool.

我的一个想法是生成一个执行转换的线程并在进程打开时间超过 n 秒时将其杀死,但我想知道是否有更简单的方法?

One idea I've had is to spawn a thread that does the conversion and kill it if the process is open for longer than n seconds, but I was wondering if there was a simpler way?

推荐答案

确定 Word 是否认为文件已损坏的唯一可靠方法是让 Word 打开它:-).我不认为任何 3rd 方应用程序在这方面是 100% 可靠的——毕竟,文档实际上可能没有损坏,但如果 Word 认为它没有帮助你.但是,很明显,您可以检测到某些情况,例如文件大小为零等.

The only sure-fire way to determine whether Word will think that the file is corrupt is to get Word to open it :-). I don't think any 3rd-party application would be 100% reliable in this regard - after all, the document might in fact not be corrupt, but that doesn't help you if Word thinks that it is. However, clearly there are some situations you could detect, such as the file being zero-sized or suchlike.

我没有遇到很多(任何?)损坏的文件,所以我想知道您看到的损坏是否遵循您可以检测到的模式?例如,这些文件是从某个地方下载的,通常是缺少文件的后半部分还是什么?

I don't come across many (any?) corrupt documents, so I do wonder if the corruption you're seeing might follow a pattern that you can detect? For example, are these documents downloaded from somewhere and usually missing the latter part of the file or something?

无论如何,损坏的文件并不是 Word 弹出对话框的唯一原因.其他原因包括:

In any case, a corrupt file is not the only reason that Word might pop up a dialog box. Other reasons include:

  • 文件受密码保护
  • 该文件包含指向其他文件的链接
  • 该文件包含宏(它们本身可能会弹出对话框,或者可能导致出现安全警告对话框)

您可以使用 Application.DisplayAlerts 等来规避其中的一些,但不是全部(尤其是安全警告).

You can circumvent some of these using Application.DisplayAlerts, etc. but not all (especially the security warning).

我在使用检测 Office 拥有的对话框的第二个线程方面取得了一些成功,并且(对于它识别的那些)按下适当的按钮.它并不优雅,但确实有效.是的,如果执行某些操作的时间太长,我的第二个线程也会终止应用程序.

I've had some success with using a 2nd thread that detects dialogs owned by Office and (for those that it recognizes) presses an appropriate button. It's hardly elegant, but it does work. And yes, my 2nd thread will also terminate the application if it takes too long to perform certain operations too.

这篇关于如何以编程方式判断word文档是否已损坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆