none
Test for password protected documents in a VBA macro RRS feed

  • Question

  • I have a VBA macro that walks a directory tree and opens each word document in the directory tree and does some processing on the word document and then saves it to another directory tree.  There are thousands of directories and tens of thousands of files.  Everything works fine except for one thing.  There are some password protected files in the directory tree so when I leave my macro running over night I come back and its stopped waiting for me to enter a password.

    Is there any way I can check in my macro whether a word document is password protected.  In that way I can then just skip processing those protected word documents and not have to manually remove them and restart my macro etc.

    Thanks,

    Harry

    Friday, January 2, 2015 11:08 PM

Answers

  • You can try to open the documents with a password that is guaranteed to be wrong, i.e. a password that isn't used for any of the documents. If the document has no password, Word will open it without complaining - it only checks the password if the document is password-protected. If the document does have a password, error 5408 will occur, and you can trap this error with an error handler. For example, with a variable doc of type Document and a variable strPath of type String that contains the full path of a document:

            ...
            On Error Resume Next
            Set doc = Documents.Open(FileName:=strPath, PasswordDocument:="!@#$%")
            If Err = 0 Then ' no error occurred
                ' Process the document
                ...
                ' And close it
                doc.Close SaveChanges:=True ' or False
            End If
            ...


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by Harry Spier Saturday, January 3, 2015 3:54 PM
    Friday, January 2, 2015 11:24 PM

All replies

  • You can try to open the documents with a password that is guaranteed to be wrong, i.e. a password that isn't used for any of the documents. If the document has no password, Word will open it without complaining - it only checks the password if the document is password-protected. If the document does have a password, error 5408 will occur, and you can trap this error with an error handler. For example, with a variable doc of type Document and a variable strPath of type String that contains the full path of a document:

            ...
            On Error Resume Next
            Set doc = Documents.Open(FileName:=strPath, PasswordDocument:="!@#$%")
            If Err = 0 Then ' no error occurred
                ' Process the document
                ...
                ' And close it
                doc.Close SaveChanges:=True ' or False
            End If
            ...


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by Harry Spier Saturday, January 3, 2015 3:54 PM
    Friday, January 2, 2015 11:24 PM
  • Thank you Hans.  That should do it.



    Harry Spier

    Saturday, January 3, 2015 3:55 PM