Wednesday, August 4, 2010

Unlock a VBA password protected Excel file

Ever felt the need to open a VBA protected excel file... maybe one of your old files that contained an excellent routine! How do you come out of that pain?

Important: This article is for educational purposes. Try this method for opening ONLY your own files, as I did too!

So how does Excel store the file contents - cell data including formulas and formats, conditional formatting, VBA code, etc. etc. Lets investigate. Create a new Excel file MyTest.xlsm and enter some dummy test data in the first sheet. Add some formulas and conditional formatting (if you want to really understand the details).
Let us now see how excel stores this data in the file. Open the file in notepad or a hex editor. Did you notice the first 2 characters? "PK". So Excel compresses its file contents. Now we know why there is not much difference if you compress an Office 2007 file.
Lets look into the compressed contents. Rename the file extension from .xlsm to .zip
Open the MyFile.zip file. Wow! its an extensive structure with xml files to store the workbook, worksheets, calculations, sharedstrings, etc.
This is how the XML of the Sheet1 looks
Lets explore more. Lets go back to our original file and add some VBA code to it.
Add a password and protect the VBA code.
Save the file and redo the same steps as earlier to open the xml file structure. We now have another XML file called vbaProject.bin. This is the file that I need to recover. Lets investigate further. Open this file in a Hex Editor (there are lots of free ones out there... the one I use is Hex Editor Neo at http://www.hhdsoftware.com/Products/home/hex-editor-free.html).
Search for the keyword "DPB" in the content. Remember to find it just above "[Host Extender Info]".
Once found, replace the string "DPB" to "DBx" and save the file. Now replace the edited "vbaProject.bin" file and place it back into the compressed file collection (replace with the old one). Rename the compressed file back to ".xlsm".

Try to open the file in Excel. It gives an error saying that the file contains an invalid key DBx. Hit Yes and proceed. The file opens. So far good. Now go to the VBA editor.
Next it gives another error "Unexpected Error". Proceed again by hittng OK
VBA Editor opens. Now try to open the module code. Again the "Unexpected Error". Dont lose heart... we are just there. Save the file and close it. We are all set. Your file is as good as new without the VBA password. Go check for yourself!

So Excel may NOT be encrypting the VBA file after all, rather it is only setting a flag to lock the VBA content. This is evident because we can still see the code in the vbaProject.bin file (that we had written in the VBA earlier).
Some more interesting bits. Check how Excel stores its file contents. Look into the file sharedStrings.xml. It stores the strings at a global level, not in the sheet itself. Also look into how it stores the formulas and formatting. Try out things like like dragging a cell content (like Area 1) into 5 cells (producing Area 1, Area 2, Area 3...) and see how it stores the values... using ranges.

That also says that, it may be better to access Excel data directly from these XML files rather than through the Excel application model. We could have much more control and simplified logic for storing (for instance in database), transporting (eg client machine to server), opening and displaying simple content. Many possibilities here...

Well, I was pretty pleased that today I learned something new... and an unlocked VBA code.

45 comments:

  1. Worked like a charm! Thank you!

    Is it possible to convert an exe, that was previously a xlsm, back into xlsm?

    ReplyDelete
  2. Você é muito bom! Parabéns!

    ReplyDelete
  3. How to open password protected excel file?

    ReplyDelete
  4. great work... thanks a lot...

    ReplyDelete
  5. Excellent post! I was searching everywhere for a VBA password unlocker/remover, and your instructions above worked like a charm (with the Hex Editor suggested above).

    ReplyDelete
  6. Outstanding work, m8! You saved me a *lot* of work.

    ReplyDelete
  7. Amazing! It works! Thank you so much for this :)

    ReplyDelete
  8. Simply amazing.. a brilliant post!

    ReplyDelete
  9. Hi Gautham,
    Thanks a lot.
    I tried the procedure for xls files - which are created in excel 2003.
    It does not open when we save the file with the extension of .zip.
    Is there any other method of opening these files.
    Thanks,
    Pradeep

    ReplyDelete
  10. it is not working for an excel 2010 file (.xlsm). it is dumping the vba projects and saying they have been corrupted.

    ReplyDelete
  11. Hey Buddy... Thanks a mill.. Your guidance rocked... hurray

    ReplyDelete
  12. This comment has been removed by the author.

    ReplyDelete
  13. Thanks it works like awesome..........

    ReplyDelete
  14. It works great for 2010 for xls file I open and save it as xlsm which opens fine as xls but vbaProject does not cracked properly.

    ReplyDelete
  15. It works for me and got good knowledge as well. I circulate to my collegues as well.

    it is very useful.

    ReplyDelete
  16. Its Works fine!! Meus Parabens. Abraços do Brazil

    ReplyDelete
  17. I won't be able to recover the password. I'll be very obliged if you crack my vba's password. Kindly send me your email address at arshad.ali91@hotmail.com

    ReplyDelete
  18. I won't be able to recover the password.
    i will be very thankful to you if help me on this.
    please send me your email.
    huziraj@gmail.com

    ReplyDelete
  19. Thank you very much!
    Its working now for me!
    Great Post!

    ReplyDelete
  20. Hi Gautam,

    Is working fine, is just fantastic!

    Thank you so much!

    P.s There is a way to recover the password?

    ReplyDelete
  21. Thanks a ton...it worked gr8....cab u tell mw how to open a file which i s password protected

    ReplyDelete
  22. Thanks a lot. Worked like a charm.

    ReplyDelete
  23. Hi, this trick is amazing and it work for me for long time, but not work anymore in Windows 7 x64 and Office 2013 x64.
    I have compared files one protected and other not protected and I find 2 XML files and vbaProject.bin with some different code lines in it.
    There are another project about it?
    Thanks a lot for to answer me!

    Vilmar

    ReplyDelete
  24. Thanks a lot.
    damet garm
    in kharejia ajab mokhi daran

    ReplyDelete
  25. Thanks for the info. I wish I found it earlier. A few years back, your company had some locked VBA projects created by a fired employee. We had to use the service called www.passsword-find.com to remove the VBA passwords - we had to buy a license to use the service, but it did well and did the trick. Hope this information will prove useful to someone if you do not succeed with this method..

    ReplyDelete
    Replies
    1. Stop advertising that software all over the web. IT DOESN'T WORK!!!

      Delete
  26. This comment has been removed by the author.

    ReplyDelete
  27. Happy to see everyone can get that... but sorry i couldnt get that. I search for DPB but text not found I am using Free Hex Editor Neo. Did I miss anything... Anybody pls help me out ???

    ReplyDelete
  28. This comment has been removed by the author.

    ReplyDelete
  29. Yes it works.. Gotcha.. Superb
    @Newton : Dude just read the above blog completely and follow the exact steps. I am dead sure, it will surely works for you also.

    ReplyDelete
  30. Get Best VBA Password Recovery Software to recover VBA password via this link http://gallery.technet.microsoft.com/VBA-Password-Recovery-eac07070

    ReplyDelete
  31. Recover VBA Password http://mail2web.com/plugins/addons/addins-for-excel/password-recovery/vba-password-recovery_1206.html

    ReplyDelete
  32. That's why we have genius for. Tks man. The best.

    ReplyDelete
  33. Dear Gautam, thanks a lot. This is very simple to understand and easy to use. Great!

    ReplyDelete
  34. To unlock excel protectionuse perfect excel password unlocker software. Through this tool you can easily open locked excel sheet and make excel sheet unprotected
    Read More............
    http://www.unlockexcelpassword.com/

    ReplyDelete
  35. Awesome. It works perfectly. Thanks Gautam !!

    ReplyDelete
  36. Excel Unlocker software easily recover Excel file password and Unlock Excel File after then open MS Excel file fastly. This software to remove Excel file password and 100% save Excel file database. It simply works on such MS Excel file versions.

    Read More-: http://www.excelunlocker.com

    ReplyDelete
    Replies
    1. Yes,
      I used This software to recover my Excel password & unlock Excel spreadsheet within just few minutes.

      PDS ( Perfect Data Solutions )

      Thanks You

      Delete