Open locked Excel file

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Open locked Excel file

Post by Rudi »

Hi,

I have some old Excel files that I protected with a file password some time back. I cannot remember the file password and am struggling to open the file now? :(

I do have a long'ish list of passwords that I store in a txt file that I have used to protect my files in the past and have successfully opened previous files with this list using the password recovery program as seen here: http://www.freewordexcelpassword.com/.

The problem I have now is that this program only works with xls extensions and not xlsx extensions. Is there a way to create a macro to reference my txt list of passwords and try each one in turn to try open my file? I can set up the basic macro code to open the xlsx file, but i have little experience in coding to reference a text file and loop through a list of entries to try.

Any help/guidance will be appreciated.
TX

Note: I know this is a sensitive topic, but please note, these are my own files and I am not trying to hack into anything!!
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Open locked Excel file

Post by HansV »

Try this; you can add bells & whistles if you like:

Code: Select all

Sub Try2Open()
    ' Modify the constants as needed
    Const strTextFile = "C:\List\MyPasswords.txt"
    Const strWorkbook = "C:\Excel\MyWorkbook.xlsx"
    Dim f As Integer
    Dim strPassword As String
    Dim wbk As Workbook
    f = FreeFile
    Open strTextFile For Input As #f
    On Error Resume Next
    Do While Not EOF(f)
        Line Input #f, strPassword
        Set wbk = Workbooks.Open(Filename:=strWorkbook, Password:=strPassword)
        If Not wbk Is Nothing Then
            MsgBox "Success!", vbInformation
            Exit Do
        End If
    Loop
    Close #f
End Sub
Best wishes,
Hans

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Open locked Excel file

Post by Rudi »

AWESOME! It worked!!!
And now I know what password I used too as I modified the msgbox to read: MsgBox "Success with password: " & strPassword & "!", vbInformation

TX for the code Hans!!

BTW: Could this be modified to open Word files too?
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Open locked Excel file

Post by HansV »

Yes, like this (macro to be run in Word):

Code: Select all

Sub Try2OpenDoc()
    ' Modify the constants as needed
    Const strTextFile = "C:\List\MyPasswords.txt"
    Const strDocument = "C:\Word\MyDocument.docx"
    Dim f As Integer
    Dim strPassword As String
    Dim doc As Document
    f = FreeFile
    Open strTextFile For Input As #f
    On Error Resume Next
    Do While Not EOF(f)
        Line Input #f, strPassword
        Set doc = Documents.Open(FileName:=strDocument, PasswordDocument:=strPassword)
        If Not doc Is Nothing Then
            MsgBox "Success with password " & strPassword, vbInformation
            Exit Do
        End If
    Loop
    Close #f
End Sub
Best wishes,
Hans

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Open locked Excel file

Post by Rudi »

That's a FAST reply...
Many TX for the assistance!
Cheers
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.