Applescript – Open password protected excel file

applescriptms officepassword

Is there a way for Applescript to open an Excel workbook (.xls) that is password protected?

I see that in the library there are functions to check if a workbook has password, but I do not know the proper syntax for opening a wb with password protection.

Tell application "Microsoft Excel"
  -- set theWb to path of .... etc.
  open theWb with password "abc" ? <--- what should go here?
end tell

Best Answer

There is a way to open a Excel workbook (xlsx, in my example) which is protected either the workbook and/or the sheet. You can use the following script:

set passwd to "1234" -- whatever you want
set theWbpath to "/Users/xxxxx/Desktop/number1.xlsx" -- example path
set theWb to "number1.xlsx" -- example file
set theSheet to "sheet1" -- example name of sheet
tell application "Microsoft Excel"
    open theWbpath                
    unprotect workbook theWb ¬ --> 
        password passwd        --> workbook part, can be left out
    unprotect sheet theSheet ¬ --> 
        password passwd        --> sheet part, can be left out
end tell