Opening external file & program via script.

Posted by Magnum on Mon 04 Feb 2002 08:36 PM — 3 posts, 20,017 views.

Canada #0
I'm feeling a tad lazy about doing the research to answer this myself. I'm betting someone here can answer it for me off the top of their head:

I want to use a script routine to open an MS Excel spreadsheet. At the moment, I am not looking to interact with the spreadsheet via script... I simply want to type an alias to launch Excel with a particular spreadsheet loaded.

The alias should be able to open a file in a defined path. i.e., not in the mushclient directory.

Thanks in advance. :)
#1
I don't know VBScript, but perhaps I can help you on your way. I believe you have to use OLE to do what you want. The best way to find OLE methods and properties is to open Excel, choose Tools -> Macro -> Visual Basic Editor. Then within the Editor, choose View -> Object Browser. The top level Excel object is Excel.Application.

After pooting around a few minutes, I found the objects that I think you need. Here is the code in Perl, you'll have to figure out how to port it to VBScript.


use Win32::OLE;

$Class = "Excel.Application";

$Excel = Win32::OLE -> GetActiveObject( $Class );
if ( !$Excel ) {
	$Excel = Win32::OLE -> new( $Class, \&QuitApp ) 
		|| die "Could not create an OLE '$Class' object";
}

$Excel  -> Workbooks -> Open("c:/foo/test.xls");
$Excel -> { Visible } = 1;

# Included for completeness.
sub QuitApp {
	my( $ComObject ) = @_;
	print "Quitting " . $ComObject -> { Name } . "\n";
	$ComObject -> Quit();
}


HTH,
Dub
Australia Forum Administrator #2
There are other examples of creating COM objects in VBscript in this forum, see Question regarding the log file for one of them.