Jump to content
Sign in to follow this  

Password Protected Word documents

Recommended Posts


This is driving me nuts! I'm trying to write a script that opens up all the word documents in a set of folders and simply re-save them. This works great until I hit a password protected file, where the AutoIt script (using Word.au3) seems to go into an eternal loop and I have to end-task to kill WINWORD.

What I would like to do is detect if the word document is password protected, then skip it if it is. Unfortunately the only way to do that is to pass a bogus password and trap the error code Word returns. When I try this:

$file = "c:\myfile.doc"
$oWordApp = _WordCreate("", 0, 0)
$oDoc = _WordDocOpen($oWordApp, $file,0,0,0,0,0,"this is a bogus password that nobody would use")

AutoIt craps out saying:

C:\Program Files\AutoIt3\Include\Word.au3 (459) : ==> The requested action with this object has failed.: 
$o_doc = $o_object.Documents.Open ($s_FilePath, $f_ConfirmConversions, $f_ReadOnly, $f_AddToRecentFiles, $s_PasswordDocument, "", $f_Revert, $s_WritePasswordDocument, "", $i_Format) 
$o_doc = $o_object.Documents.Open ($s_FilePath, $f_ConfirmConversions, $f_ReadOnly, $f_AddToRecentFiles, $s_PasswordDocument, "", $f_Revert, $s_WritePasswordDocument, "", $i_Format)^ ERROR

Any ideas? I've also tried using:

$file = "c:\myfile.doc"
$word = ObjCreate("word.application")
If IsObj($word) Then
    $o_doc = $word.Documents.Open($file,False,False,False,"this is a bogus password that nobody would use")     

Same results ("The requested.....has failed").

Share this post

Link to post
Share on other sites

Well I found a work-around, albeit not very graceful. I ended up writing a VB6 ActiveX dll that you pass the file path to and it will return true if the document is password protected and false if not. I don't much like this fix because now the DLL has to be registered/installed on any client that runs this script, but it will work.

For those interested here's what I did:


;First unregister then register the dll (makes sure it is registered)
; register the document checker dll 
Runwait(@SystemDir & '\regsvr32.exe  /s/u ' & @ScriptDir & '\documentChecker.dll',"",@SW_HIDE)
Runwait(@SystemDir & '\regsvr32.exe  /s ' & @ScriptDir & '\documentChecker.dll',"",@SW_HIDE)

If Not isPasswordProtected($file) Then                  
   ...process doc 

Func isPasswordProtected($file)

    $obj = ObjCreate("documentChecker.checkDocument")
    If IsObj($obj) Then
        $ret = $obj.isPasswordProtected($file)
        ConsoleWrite("Error initializing object")
    Return $ret         

Then the documentChecker.dll is a project named documentChecker with a single class (named checkDocument):


Public Function isPasswordProtected(ByVal strFile As String) As String  
    Dim oDoc As Document
    ' Set Error Handler
    On Error Resume Next
    ' Attempt to Open the Document
    Set oDoc = Documents.Open( _
               FileName:=strFile, _
    Select Case Err.Number
        Case 0
            ' Document was Successfully Opened
            Debug.Print strFileName & " was processed."
            isPasswordProtected = "False"
        Case 5408
            ' Document is Password-protected and was NOT Opened
            Debug.Print strFileName & " is password-protected " & _
                "and was NOT processed."
            isPasswordProtected = "True"
        Case Else
            ' Another Error Occurred
            isPasswordProtected = "True"
    End Select
    ' Close Document
    ' Clear Object Variable
    Set oDoc = Nothing
End Function

As long as the DLL is distributed in the @ScriptDir it works fine.

If anyone has a prettier way to do this I am all ears!

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.