{"id":14557,"date":"2017-10-24T21:12:44","date_gmt":"2017-10-24T21:12:44","guid":{"rendered":"http:\/\/learnexcelmacro.com\/wp\/?p=14557"},"modified":"2017-10-25T08:11:48","modified_gmt":"2017-10-25T08:11:48","slug":"vba-to-open-a-password-protected-workbook","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2017\/10\/vba-to-open-a-password-protected-workbook\/","title":{"rendered":"VBA to Open or Create a Password Protected Workbook"},"content":{"rendered":"
Dear Friends,<\/p>\n
Previously I have written about how to protect and un-protect a Sheet in a Workbook<\/strong><\/a>. In this article, I am going to cover all about protecting and unprotecting a workbook.<\/p>\n This is going to be a short and simple article, yet informative and useful for your day to day VBA programming.<\/p>\n In this article, I am going to share with you VBA code to: There is no special method for doing this. Rather it is simply done by passing the password parameters<\/em> in Workbooks.Open() <\/strong><\/a>and Workbook.SaveAs()<\/strong><\/a><\/p>\n You can refer in the below code.<\/p>\n Let’s take an example where you want to save an Excel Workbook as password protected.<\/p>\n You can use following VBA code to open a password protected workbook to read or write.<\/p>\n Password: <\/strong> This is the password to lock the workbook from opening it. If your workbook is locked with both the passwords – For opening and For editing<\/em><\/strong> – and you skip passing the password for editing, then you get a popup like below.
\n1. Open a password protected Excel Workbook.
\n2. Create a password protected excel workbook.<\/p>\nHow to create a password protected workbook<\/h1>\n
\r\n
\r\nFunction SaveWorkbookAsPasswordProtected\r\n ActiveWorkbook.SaveAs Filename:=\"...\\protected.xlsx\", Password:=\"1234\", WriteResPassword:=\"1234\"\r\nEnd Function\r\n<\/code>\r\n<\/pre>\n
How to Open a password protected workbook<\/h1>\n
\r\n
\r\nSub openPasswordProtectedWorkbook()\r\n Workbooks.Open Filename:=\"...\\protected.xlsm\", Password:=\"1234\", WriteResPassword:=\"1234\"\r\nEnd Sub\r\n<\/code>\r\n<\/pre>\n
Important:<\/h2>\n
\nWriteResPassword: <\/strong> This is the password to lock the workbook from EDITING it.<\/p>\n
\nYou can ofcourse open that workbook in write mode by providing the correct password or in edit mode by clicking on the button shown on the popup.
\n