Scripting Excel

I have a script that opens an Excel file and updates some information from the user.

However, if Excel is not running it opens it. (fine) but this makes the Excel splash screen open, and my script gets stuck.

Is there any way to open excel without having the splash screen?

tell application "Microsoft Excel"
	Activate
	Open "myFile.xls"
	Select Sheet "MainSheet"
	set LastRow to ("R4C2") 
...

end tell


Thanks,

Han Solo

Does it help to add the launch command? This sometimes opens an app without going through the initialization routine.

tell application "Microsoft Excel"
	launch
	Activate
	Open "myFile.xls"
	Select Sheet "MainSheet"
	set LastRow to ("R4C2") 
...

end tell

Thanks.

Han Solo

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

Hi Han,

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

Hello.

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


-- © McUsr 2010 and put in the Public domain without any restrictions
on initiateExcel()
	local pName
	tell application "System Events" to set pName to name of processes
	if "Microsoft Excel" is not in pName then
		tell application "Microsoft Excel"
			launch
		end tell
	end if
	
	tell application "Microsoft Excel"
		local a
		try
			set a to get count of its every window
		on error
			tell me
				activate
				display alert "You must close Microsoft Excels's Project Chooser Window in order to run this script\nPlease close the window and rerun the script "
				error number -128
			end tell
		end try
	end tell
end initiateExcel

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.

Hello!

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