I am maintaing an archiving system that has to convert various kinds of document formats to tif. My problem is with password-protected Word-documents. If the document is password-protected, then Word responds with a popup asking me to enter the password. It's ok if the document is password-protected if I can just tell the customer that he needs to do something about it. The problem is that I programmatically cannot register if Word is prompting for a password. The code below is the standard interop way of opening a document without a password. If I enter no password or a wrong one, then I am prompted by Word visually via a popup. Is there any other way for me than using AutoHotKey to look for the popup-window? It would be ok if I could look inside the doc-file for a string or character which tells if it is protected or not.
// 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);
Solution:
It is possible to do it in a VBA macro in Word. So in order to do it from C#, then you would create the macro from C# and execute it. I haven't tried it. But here is the code:
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