AppleScript to Open a Password Protected Excel File

I am trying to open a password protected Microsoft Excel file using AppleScript.

I have written two different AppleScripts and both “fail” when trying to enter the workbook password.

The first script is as follows:

tell application "Microsoft Excel"

    activate

    set TheFileToOpen to "/Users/JoelC/Documents/Projects On The Go/Budget and Spend Tracking/20140101_budget and expenditure.xlsx"
    set TheWorkBook to "20140101_budget and expenditure.xlsx"
    set TheWorksheet to "Budget_2021"
    set ThePassword to "abcdefgh"

    open TheFileToOpen
    tell application "System Events" in window "Password" of "Microsoft Excel" to keystroke ThePassword
end tell

The second script is as follows:

tell application "Microsoft Excel"
    activate

    set TheFileToOpen to "/Users/JoelC/Documents/Projects On The Go/Budget and Spend Tracking/20140101_budget and expenditure.xlsx"
    set TheWorkBook to "20140101_budget and expenditure.xlsx"
    set TheWorksheet to "Budget_2021"
    set ThePassword to "abcdefgh"

    open TheFileToOpen
    unprotect workbook TheWorkBook password ThePassword
    unprotect sheet TheWorksheet password ThePassword		
end tell

I would GREATLY appreciate any and all help in getting this to work.

Thanks!

Try the Open Workbook command. One of the parameters is password.

I haven’t tried it, but it looks promising. (just click the “Open in Script Debugger” button and the command should display properly.

tell application "Microsoft Excel"
			set #~result_Workbook~# to open workbook workbook file name #~Text~# ¬
				update links #~custompicker:MyUDateLinks:do not update links:update external links only:update remote links only:update remote and external links~# ¬
				read only #~Boolean~# ¬
				format #~custompicker:MyODelimiter:tab delimiter:commas delimiter:spaces delimiter:semicolon delimiter:no delimiter:custom character delimiter~# ¬
				password #~Text~# ¬
				write reserved password #~Text~# ¬
				ignore read only recommended #~Boolean~# ¬
				origin #~custompicker:XlPlatform:Macintosh:MSDos:MSWindows~# ¬
				delimiter #~Text~# ¬
				editable #~Boolean~# ¬
				notify #~Boolean~# ¬
				converter #~Number~# ¬
				add to mru #~Boolean~#
		end tell

@estockly, again, thank you for taking the time to respond. The above suggestion worked PERFECTLY.

Very much appreciated!

1 Like