0

I am trying to create a function that would check if docx or pptx files are password protected. Here is my function:

 Public Function isPasswordProtected(ByVal path As String) As Boolean
    On Error GoTo ErrorHandler
    Dim wordApp As Word.Application
    Dim wordFile As Object
    
    
    Set wordApp = New Word.Application
    wordApp.Visible = False
 
    Set wordFile = Documents.Open(fileName:=path, PasswordDocument:="!@#$%")
    If Err = 0 Then ' no error occurred
        isPasswordProtected = False
    Else
        isPasswordProtected = True
    End If
    
    wordFile.Close (False)
    wordApp.Quit
    Set wordApp = Nothing
    Set wordFile = Nothing
    isPasswordProtected = True
ErrorHandler:
    Debug.Print "isPasswordProtected( ):" & Err.Description
End Function

Sub TestProtection()

    Dim protected As String
    Dim unrpotected As String
    
    protected = "C:\Temp\word\protected.docx"
    Debug.Print isPasswordProtected(protected)
    unrpotected = "C:\Temp\word\unprotected.docx"
    Debug.Print isPasswordProtected(unrpotected)
    
End Sub

I does work but not reliably from time to time it just throws the following error: isPasswordProtected():The remote server machine does not exist or is unavailable

Rather than:

isPasswordProtected( ):The password is incorrect. Word cannot open the document.
 (C:\Temp\word\protected.docx)
False
isPasswordProtected( ):
True

And when I am checking lots of documents its a problem. Are there any alternative ways of doing that?

chris neilsen
  • 52,446
  • 10
  • 84
  • 123
Andrius Solopovas
  • 967
  • 11
  • 41
  • That error will probably go away if you use proper referencing, i.e.`Set wordFile = WordApp.Documents.Open(fileName:=path, PasswordDocument:="!@#$%")` – Spectral Instance Feb 23 '23 at 17:39
  • it kind of does and doesn't it keeps giving me True on documents that are not protected – Andrius Solopovas Feb 23 '23 at 17:46
  • Does this answer your question? [Check if a Word Document is password protected without opening it](https://stackoverflow.com/questions/37190539/check-if-a-word-document-is-password-protected-without-opening-it) – Eugene Astafiev Feb 23 '23 at 17:53
  • 1
    That's because, when no error occurs you conditionally set the return value to `False`, only to 'constantly' overwrite the return value with `True` right before the error handler...(a function does **not** exit as soon as the return value has been assigned) – Spectral Instance Feb 23 '23 at 18:12

1 Answers1

1

Try this version of your function

 Public Function isPasswordProtected(ByVal path As String) As Boolean
    On Error GoTo ErrorHandler
    Dim wordApp As Word.Application
    Dim wordFile As Word.Document
    
    
    Set wordApp = New Word.Application
    wordApp.Visible = False
 
    Set wordFile = wordApp.Documents.Open(Filename:=path, PasswordDocument:="!@#$%")
    If Err = 0 Then ' no error occurred
        isPasswordProtected = False
    Else
        isPasswordProtected = True
    End If
    
    wordFile.Close (False)
    wordApp.Quit
    Set wordApp = Nothing
    Set wordFile = Nothing
Exit Function
ErrorHandler:
    Debug.Print "isPasswordProtected( ):" & Err.Description & vbNewLine
    wordApp.Quit
    Set wordApp = Nothing
    Set wordFile = Nothing
    isPasswordProtected = True
End Function
Spectral Instance
  • 1,971
  • 2
  • 3
  • 6
  • 1
    Also, there shouldn't be any parentheses in `wordFile.Close (False)`, and `Set wordApp = Nothing` and `Set wordFile = Nothing` are unnecessary. – BigBen Feb 23 '23 at 18:29
  • @BigBen, I agree that those parentheses look anomalous but the code still works _with_ them; as for nullifying objects, yes, it's not necessary, but there would be more better programmers if some best practices _were_ necessary – Spectral Instance Feb 23 '23 at 18:39
  • 1
    The parentheses *shouldn't* be there at all, and are quite a bad practice. – BigBen Feb 23 '23 at 18:41