如何查看Word文件是否受密码保护?

我正在维护一个归档系统,必须将各种文档格式转换为tif。 我的问题是密码保护的Word文档。 如果文档受密码保护,则Word会弹出一个弹出窗口,要求我输入密码。 如果我可以告诉客户他需要对此做些什么,那么文档是否受密码保护是可以的。 问题是,如果Word提示输入密码,我将无法以编程方式注册。 下面的代码是在没有密码的情况下打开文档的标准互操作方式。 如果我没有输入密码或错误密码,那么我会通过弹出窗口通过Word提示我。 除了使用AutoHotKey查找弹出窗口之外,还有其他方式吗? 如果我可以在doc文件中查找一个字符串或字符来告诉它是否受到保护,那就没问题了。

// Open the document... this.document = wordApplication.Documents.Open( ref inputFile, ref confirmConversions, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref visible, ref missing, ref missing, ref missing, ref missing); 

解:

可以在Word中的VBA宏中执行此操作。 所以为了从C#中创建它,那么你将从C#创建宏并执行它。 我没试过。 但这里是代码:

 Sub MyMacro() Dim oDoc As Document On Error Resume Next Set oDoc = Documents.Open(FileName:="C:\MyFile.doc", PasswordDocument:=password) Select Case Err.Number Case 0 Debug.Print "File was processed." Case 5408 'Debug.Print "Wrong password!" Case Else MsgBox Err.Number & ":" & Err.Description End Select