Urgent - Detecting password protected documents

I am automating Word 97 using VB6 (sequentially opening up a bunch of documents and doing stuff).

When I come across a document that has been password protected, the 'Enter Password' dialog is displayed. This stops my VB application until the user responds.

My problem is that I don't want the user to have to respond. I need a way to check if the document is password protected, and if so, simply move on to the next document.

Therefore, does anyone know of a way to check for a password before attempting to open the document, or of automatically cancelling the dialog so it doesn't stop my application.

The DisplayAlerts method doesn't do the job.

I have also posted this question to the Office section, so first-in with the correct answer from either section wins the points.
LVL 3
pacificdbAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

richardlouieCommented:
Use the HasPassword property.

Try the following:

   If ActiveDocument.HasPassword = True Then
       MsgBox "The password is set"
   End If

Hope this helps.

Rich

0
pacificdbAuthor Commented:
Thanks - nice try, but consider the following:

When my application executes the following line of code on a password-protected document, 'doc' has no value because the document has not been opened (Word is waiting for the user to enter the password into the dialog). I cannot use the HasPassword method because there is no ActiveDocument yet.

Set doc = appWord.Documents.Open(strFName, , True)
0
richardlouieCommented:
You're right.  

Well, as a hack, you try passing a bogus password and check if you get the "Error 5408 - The password is incorrect" message and handle it through an error handler.  If the file doesn't have a password, then passing the bogus password has no effect on the file.

Here's some code that worked for me:

=======================================

Private Sub Command2_Click()

Dim appWord As Word.Application
Dim doc As Object
Dim strFName As String
Dim sDate As String
Dim sNote As String
Dim bError As Boolean
 
    On Error GoTo ErrorHandle
   
    Set appWord = CreateObject("Word.Application")
   
    sDate = CStr(Now)

    '
    ' Start opening the files.
    '
    For n = 1 To 6
       
        '
        ' There are 6 files. 3 are
        ' password protected and 3
        ' do not have a password.
        '  
        strFName = "E:\Development\Test\Print\Password" & n & ".doc"

        '
        ' Open the document and pass a
        ' bogus password to see if the
        ' file is password protected.
        ' If it is, then we ignore it
        ' and continue on with the next
        ' file.
        '
        Documents.Open strFName, PasswordDocument:="XXXXXXX"

        If Not bError Then
            sNote = sDate & ": Added this line through the program."
            Selection.TypeText Text:=sNote
            Selection.TypeParagraph
            Documents.Close
        Else
            bError = False
        End If
       
    Next n
   
    Exit Sub

ErrorHandle:
        If Err.Number = 5408 Then
            MsgBox strFName & " is password protected."
            bError = True
            Resume Next
        End If

End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

amebaCommented:
Cool hack!
0
pacificdbAuthor Commented:
Thanks richardlouie,

Your second attempt, although indelicate, certainly serves the purpose. Considering the way I'm doing things, your suggested addition of the password argument works without any noticeable problems or time penalty, which was of major concern.

Thanks...

0
In-Q-TelCommented:
this solution used to work, but in Word 2003, it no longer works: what appears to be happening is that supplying a bogus password always causes error 4198 ("Command Failed") whether the document has a password or not
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.