View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Jake Marx[_3_] Jake Marx[_3_] is offline
external usenet poster
 
Posts: 860
Default How to check for password in an excel file.

Hi Aatash,

Aatash wrote:
I would like to know how I can check whether an excel file
is password protected or not.


I'm not sure if you can determine that without attempting to open the file.
If you specify a password when opening a workbook, that password will be
ignored if the workbook is not password-protected. If the workbook is
password-protected and the password you supply is incorrect, you'll get a
1004 runtime error, which you can trap:

Sub test()
On Error GoTo ErrHandler

Workbooks.Open Filename:="c:\book1.xls", Password:="$$$$"

ExitRoutine:
Exit Sub
ErrHandler:
If Err.Number = 1004 And InStr(1, Err.Description, _
"Password", vbTextCompare) Then
MsgBox "Password-protected workbook. Unable to open.", _
vbExclamation, "Error"
Else
MsgBox CStr(Err.Number) & ": " & Err.Description
End If
Resume ExitRoutine
End Sub


--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]