Excel Auto Recovery


I was wondering if anyone has encountered this and found a workaround in Applescript. I have a script that autogenerates an Excel spreadsheet (using Excel 2011) by pasting formulas and inserting images, etc… into my Excel file. One problem I’m encountering is that my Applescript is crashing because Excel crashes. So to compensate for the random crashing of either Excel or my Applescript I wrote another script that is launched every 30 seconds using launchd that monitors the CPU usage of my Excel script. If it sees the CPU usage <=0.1 then I know that Excel or my Excel script has died so I kill the Excel process and my Excel Script process and restart them. This is all working fine has well with the exception of one scenario where Excel is trying to do Auto Recovery when it restarts and it’s prompting me “Excel saved changes to the file before the application quit unexpectedly. Do you want to open the last saved version of this file?” I can either click on Cancel or Open.

I’ve tried to minimize Excel crashing in the first place by putting in some delays in my script and that has helped somewhat but Excel still seems to crash as my Excel script is pretty big and doing a lot of pasting and generating formulas.

I’ve tried setting the Preferences in Excel to not do Auto Recovery but it doesn’t seem to work. Once in a while I still get the prompt by Excel. Anyone know how to bypass this prompt within Excel when it starts up so my Excel script can continue? Or the best way to handle this prompt and click on cancel?


I don’t have Excel (I use Numbers), so I can’t give you an exact answer, but I would use a try statement or an if clause to test if that message is popping up and then click cancel with GUI scripting. Something like:

	tell application "System Events" to tell process "Excel"
		click button "Cancel" of front window
	end tell
end try

It may not be as simple as “window 1,” however. You can use code like this (which is greatly assisted by using UI/Accessibility Inspector) to find the hierarchy of the button you need to click:

tell application "Excel"
	if front window is not myFileName then
		tell application "System Events" to tell process "Excel"
			display dialog (UI elements of window 1)
		end tell
	end if
end tell

Hope that helps.