Two problems with Microsoft Excel scripting.

Hi all,

I have two problems with Microsoft Excel scripting.

  1. I’d like to check if the front worksheet is saved. I’ve come up with this:
return Saved of ActiveWorkbook

which works, but it’s not consistent with other apps. In particular, the following doesn’t work for me:

 return Modified of ActiveWorkbook

My question is, why not? “Modified” is in the Excel AppleScript dictionary. Any ideas?

  1. In the case that my script detects that the sheet is not saved, then I’d like to have my script save it. Running the “Save” command seems to want to write over the file itself, forcing the user to click “Replace”. “Save As” forces me to provide another file name, which is not what I want. I want to simulate the press of “apple-s”.

Thanks,
Rob

There appear to be problems in Excel when saving or opening files as there are multiple save and open commands in Excel’s AS dictionary. (Open the ddictionary in Script Editor and search through it for “save”.) As a (very kludgy) workaround, you can use this code to save a modified worksheet by closing it and then immediately re-opening it:

tell application "Microsoft Excel"
	tell ActiveWorkbook
		set the_path to FullName
		set is_saved to Saved
	end tell
	if is_saved = false then
		Close document 1 saving Yes
		Open the_path
	end if
end tell

It also appears Excel has some wonkiness when it comes to file specifications/aliases. When I tried to open the path as an alias, the app balked. Feeding it a plain text string, however, worked.

Jon

A terminolgy point: A workbook gets saved as a file. A worksheet is the tird dimension of a workkbook. Try Insert Workksheet on the menu to see the difference. Each worksheet can also be named individually. The default name is something like “Sheet1”, “Sheet2”, etc., but you can click on the tab at the bottom of the worksheet and change the name.

Browse the dictionary and you’ll see that a Workbook can contain multiple worksheets. Workbook is parent to worksheet.

I doubt you can check if a Worksheet has been changed/saved. But a workbook should work.