Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if a MS Office Document is Password Protected 3

Status
Not open for further replies.

jayraykay

Programmer
Mar 10, 2004
22
US
Hello,

I am creating a web application where users upload Microsoft Office Documents to my web server.

I need to ensure that the user's do not upload password protected files.

Can I programatically check whether or not a Word or Excel file is password protected once it resides on the server?

Thanks!
J
 
Pretty easy...

Code:
Option Explicit

Private Sub Form_Load()
  Dim oXLA As Excel.Application
  Dim oWRDA As Word.Application
  Dim oXLW As Excel.Workbook
  Dim oWRDD As Word.Document
  Dim excelFilePath As String: excelFilePath = "c:\password.xls"
  Dim wordFilePath As String: wordFilePath = "c:\password.doc"
  
  Set oXLA = New Excel.Application
  Set oWRDA = New Word.Application
  
  On Error Resume Next
  
  'Excel Document
  With oXLA
    .Visible = False
    .AskToUpdateLinks = False
  End With
  Err.Clear
  Set oXLW = oXLA.Workbooks.Open(FileName:=excelFilePath, UpdateLinks:=False, ReadOnly:=True, Password:=False, WriteResPassword:=False, IgnoreReadOnlyRecommended:=True)
  If Err.Number <> 0 Then 'usually 1004
    MsgBox "Excel file is password protected!"
  Else
    MsgBox "Excel file is not password protected!"
    oXLW.Close
  End If
  oXLA.Quit
  Set oXLW = Nothing
  Set oXLA = Nothing
  
  'Word Document
  With oWRDA
    .Visible = False
  End With
  Err.Clear
  Set oWRDD = oWRDA.Documents.Open(FileName:=wordFilePath, ReadOnly:=True, PasswordDocument:=False, PasswordTemplate:=False, WritePasswordDocument:=False, WritePasswordTemplate:=False)
  If Err.Number <> 0 Then  'usually 13
    MsgBox "Word file is password protected!"
  Else
    MsgBox "Word file is not password protected!"
    oWRDD.Close
  End If
  
  oWRDA.Quit
  Set oWRDD = Nothing
  Set oWRDA = Nothing
  
  End
  
End Sub

HtH,

Rob

-Focus on the solution to the problem, not the obstacles in the way.-
 
has anyone else tried this code? i tried it and it tells me every word document is password protected.

is there something that needs to be changed or is there another way to check if a word document has is password protected?
 
Hi

I tried the code on a Word document and found the same problem as drctx. That is, the routine always labelled the file as password protected. I modified the code to the following and it ran.
Code:
Option Explicit

Private Sub Form_Load()
  Dim oWRDA As Word.Application
  Dim oWRDD As Word.Document
  Dim wordFilePath As String: wordFilePath = "c:\password.doc"
  
  Set oWRDA = New Word.Application
  
  On Error Resume Next
  
  'Word Document
  With oWRDA
    .Visible = False
  End With
  Err.Clear
  Set oWRDD = oWRDA.Documents.Open(FileName:=wordFilePath, ReadOnly:=True)
  If Err.Number <> 0 Then  'usually 13
    MsgBox "Word file is password protected!"
  Else
    MsgBox "Word file is not password protected!"
    oWRDD.Close
  End If
  
  oWRDA.Quit
  Set oWRDD = Nothing
  Set oWRDA = Nothing
  
  End
  
End Sub

Cassandra
 
Hi drctx:

I have found the following in Rob's code:

PasswordDocument, PasswordTemplate, WritePasswordDocument, and WritePasswordDocument generate Error 13 Type Mismatch when passed a value of False.

The corrected code above incurs the password entry dialog box within MS Word.



The Error checking code should be
Code:
    If Err.Number = 5408 Then    ' Password Protected File
        MsgBox "Word file is password protected!"
    Else
        MsgBox "Word file is not password protected!"
    End If
    Err.Clear
    Err.ResumeNext
    oWRDD.Close

The other nuance that I found is that the technique is slow, since Word needs to load up. Don't know whether there is a solution to this slowness problem.

Cassandra
 

Oops! Mea culpa!

The If Err.Number statement is incomplete as I posted. It should be something like:
Code:
    If (Err.Number and 5408) <> 0 Then    ' 5408 - Password Protected File

Cassandra
 
thanks for the help.
i ran the code below and word asks for the password, if i enter the password i

get the message "Word file is not password protected!", if i click cancel to the

password box i get "Word file is password protected!".

if the file is modify password protected then i get "Word file is not password

protected!".

the original code was checking for opening and write passwords those are the 2

passwords that i want to check for.

Code:
        Dim oWRDA As Word.Application
        Dim oWRDD As Word.Document
        Dim wordFilePath As String : wordFilePath = "c:\password.doc"

        oWRDA = New Word.Application

        On Error Resume Next

        'Word Document
        With oWRDA
            .Visible = False
        End With
        Err.Clear()
        oWRDD = oWRDA.Documents.Open(FileName:=wordFilePath, ReadOnly:=True)
        If (Err.Number And 5408) <> 0 Then    ' 5408 - Password Protected File
            MsgBox("Word file is password protected!")
        Else
            MsgBox("Word file is not password protected!")
        End If
        Err.Clear()
        oWRDD.Close()

        oWRDA.Quit()
        oWRDD = Nothing
        oWRDA = Nothing

        End

 
you got me on the right track and i figured it out. i opened the word file and used "000" as the passwords since this is the incorrect password it gives error number 5408.

Code:
        Dim oWRDA As Word.Application
        Dim oWRDD As Word.Document
        Dim wordFilePath As String

        wordFilePath = "c:\password.doc"

        oWRDA = New Word.Application

        On Error Resume Next
        'On Error GoTo err_handle

        oWRDA.Visible = False

        Err.Clear()
        oWRDD = oWRDA.Documents.Open(FileName:=wordFilePath, PasswordDocument:="000", PasswordTemplate:="000", WritePasswordDocument:="000", WritePasswordTemplate:="000")

        If Err.Number = 5408 Then  '5408 - The password is incorrect.
            MsgBox("Word file is password protected!")
        Else
            MsgBox("Word file is not password protected!")
            oWRDD.Close()
        End If

        oWRDA.Quit()
        oWRDD = Nothing
        oWRDA = Nothing
 
Hi drctx:

Well done! That's another note to add my files.

A star for you!

Cassandra
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top