I ended up with the following because of the delay in opening Excel, that is, Excel was not open by the time AppleScript tried to open the document:
on getWorkbook(theWorkbook)
tell application "Microsoft Excel" to open theWorkbook
tell application "Finder" to set ExcelDoc to name of theWorkbook
-- tell application "Finder" to display dialog ExcelDoc buttons {"OK"}
(*
With the following approach, un-check Excel's General Preference:
"Show Project Gallery at start up"
*)
tell application "System Events"
set allApps to name of every application process
repeat while (allApps does not contain "Microsoft Excel")
set allApps to name of every application process
end repeat
end tell
tell application "Microsoft Excel"
set allWindows to name of every window
repeat while (allWindows does not contain ExcelDoc)
set allWindows to name of every window
end repeat
end tell
tell application "Microsoft Excel" to activate
end getWorkbook
try this, it checks whether Excel is already running.
If not, it launches Excel and closes the empty document
tell application "System Events" to set pName to name of processes
if "Microsoft Excel" is not in pName then
tell application "Microsoft Excel"
activate
close workbook 1 saving no
end tell
end if
tell application "Microsoft Excel"
open "myFile.xls"
select sheet "MainSheet"
set LastRow to ("R4C2")
...
end tell
@ John Love: I’ve never had the problem, that AppleScript was not waiting until Excel has finished starting,
all my documents are opened properly only with
tell application "Microsoft Excel"
open "myDocument.xls"
end tell
I have reworked Stefan’s example a little bit to ensure that we have Excel in a state without the Project Chooser Window open, which at least makes my scripts hang.
Needless to say, this is for deploying scripts to others and ensuring that the script will not hang upon startup If they choose to run your script while having the project chooser window open. Enjoy
For what its worth, I started using this method to close the Project Gallery.
tell application "Microsoft Excel"
activate
try
set calc to calculation as text
if calc is "missing value" then -- Need to close panel
tell application "System Events"
tell process excel_name
click button "Cancel" of window "Project Gallery - New"
end tell
end tell
end if
end try
end tell
I only really need it for Excel 2004. 2008 seems to incorporate the gallery as part of the empty workbook that it creates on open. Haven’t scripted anything beyond Excel 2008.
I discovered that I could do like below to bypass opening the project gallery in Microsoft Excel 2008. In Microsoft Excel 2008 you can turn off the display of the project gallery in preferences, so it is of most use when you don’t know the users settings.
# Assuming Excel isn't running up front.
tell application "Microsoft Excel"
launch
open (path to desktop as text) & "Workbook1.xls"
activate
end tell
I don’t know if this works also in Excel 2004, but in Excel 2011 you could bypass the startup dialog temporarily with
tell application "Microsoft Excel"
launch
set startupDialogTemp to startup dialog
set startup dialog to false
-- do stuff
set startup dialog to startupDialogTemp
end tell