in reply to Excel and OLE

Two things come to mind:
  1. Excel has its own concept of 'Working directory' - It won't find the file unless it is where Excel thinks it should be
  2. The infinite loop at the end is preventing OLE from shutting Excel down properly
For #1, convert the filename to its full path:

use File::Spec; my $excelfile = File::Spec->rel2abs($ARGV[0]);

Excel will be able to find and open the file. Take out the 'Quit' parameter to new and get rid of the while (1){}, and Excel will stay open so user can work with the spreadsheet.

As far as returning control to the perl script after the user is done with Excel, you are on your own ;) It will be difficult to tell when the user is done with the spreadsheet, especially if Excel is already open.

Replies are listed 'Best First'.
Re: Re: Excel and OLE
by Silicon Cactus (Scribe) on Sep 18, 2002 at 21:00 UTC
    You should be able to "watch" the excel window using Win32::API, once you get the window handle.

    You could conceivably just check for the existence of the window handle repeatedly in a loop, breaking out of it when the handle is destroyed. Just make sure to stick a sleep statement in there so you don't max out your processor in the loop.
      Right you are! The FindWindow API lets you search by window class or exact window title, or you can use EnumWindows and check title bar text for a partial match.

      I was trying to point out the potential pitfalls without being too wordy (or too discouraging - there might be a better way to do it;). The original snippet grabs the existing instance of Excel if it is running. If it was already open and the user closes the document, Excel is still running. Or if the end user forgets to close Excel (or maybe just minimizes it and thinks it is closed) Excel is still running - with the same Window class, maybe even with the same window title.

      I did something like this once in VB and it turned into a major support headache for me.