How can I check if Word password is protected?

I support an archiving system that should convert various types of document formats to tif. My problem is with password protected Word documents. If the document is password protected, then Word responds with a pop-up window, requiring you to enter a password. This is normal if the document is password protected, if I can just tell the client that he needs to do something. The problem is that I cannot programmatically register if Word asks for a password. The code below is the standard way to interact with a document without a password. If I do not enter a password or an erroneous one, then I will be visually requested by Word through a pop-up window. Is there any other way for me than using AutoHotKey to search for a popup? It would be nice if I could look into the doc file for a string or character that says whether 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);

Decision:

This can be done in the VBA macro in Word. Therefore, to do this with C #, you must create a macro from C # and execute it. I have not tried. 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
+3
source share

All Articles