• Status: Solved
  • Priority: High
  • Security: Public
  • Views: 28
  • Last Modified:

How To Determine Excel File is Password Protected

How can I use VBA in an Excel file to determine if another Excel file is password protected, preferably without opening the file?
Chuck Wood
Chuck Wood
1 Solution
Wayne Taylor (webtubbs)Commented:
It's not possible without attempting to open the workbook. But you can try opening the file using a dummy password and it will return an error if the password is wrong. If it's not password protected, it will open as normal despite providing a password.

Function IsPasswordProtected(sFile As String) As Boolean

    On Error Resume Next
    Workbooks.Open(Filename:=sFile, Password:="dummypassword").Close SaveChanges:=False
    If Err.Number Then
        IsPasswordProtected = True
    End If
End Function

Sub test()

    MsgBox IsPasswordProtected("C:\tests\Book1.xlsx")
End Sub

Open in new window

Chuck WoodAuthor Commented:
Excellent answer. Thank you.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now