I try to open password protected .xlsx
files (Excel 2007 format) without typing the password manually. I have installed Excel 2003 and the Microsoft Office Comp
This may be late, but for any future person with Interop, for me it worked like this:
To open to write
var WFile = new Excel.Application();
Excel.Workbook Wbook = WFile.Workbooks.Open("myFilepath", ReadOnly: false, Password: "mypassword");
To open as read-only
var WFile = new Excel.Application();
Excel.Workbook Wbook = WFile.Workbooks.Open("myFilepath", ReadOnly: true, Password: "mypassword");
The Answer by @J1mm1995 works when trying to open files as readonly, and fails to open some excel files when you want to open the excel file for modification(ReadOnly is set to false).
I understood that this was because the Workbooks.Open() method also expected you to specify the WritePassword. The following code worked for me:
var WFile = new Excel.Application();
Excel.Workbook Wbook = WFile.Workbooks.Open(path, ReadOnly: false, Password: "mypassword", WriteResPassword: "mypassword");