in reply to Is an Excel spreadsheet open?

I'd ask Excel, through Win32::OLE. Most likely, iterating over Application.Workbooks or somesuch should yield all filenames of all open files.

Replies are listed 'Best First'.
Re^2: Is an Excel spreadsheet open?
by merrymonk (Hermit) on Feb 10, 2010 at 16:50 UTC
    Thanks for that. I looked in the link and found enum for all objects and thought that may give what I wanted.
    Therefore I wrote some Perl (below) to test it.
    Unfortunately, the count was always zero no matter how many Excel files were open.
    Googling for Excel and Enum, I did find a site where someone was trying gain access to a file by requesting a handle to it.
    I thought that this may give what I wanted on the basis that it would only return a handle if the file was there but not opened.
    I can easily test if a file is there – if it is not it cannot be open.
    This is also in the Perl code below. Unfortunately this did not work either since it always returned a handle and if the file was not open, it opened Excel with a ‘blank’ screen.
    Can anyone give me a clue as to what to do next?
    use OLE; use Win32::OLE::Const "Microsoft Excel"; use strict "vars"; my ($Count, $file, $ML); $Count = Win32::OLE->EnumAllObjects(sub { my $Object = shift; my $Class = Win32::OLE->QueryObjectType($Object); printf "# Object=%s Class=%s\n", $Object, $Class; }); print "count <$Count>\n"; $file = "C:\\abcd.xlsx"; $ML = Win32::OLE->GetObject($file) or print "can't get a handle on '$file'"; print "$ML\n";

      Again, why don't you ask Excel about its open files?

      # From memory, as I don't have Excel at hand currently use Win32::OLE 'in'; my $excel = Win32::OLE->CreateObject('Excel.Application'); for my $wb (in($excel->{Workbooks})) { print $wb->{Filename},"\n"; };
        Thanks again.
        I tried what you have most recently suggested as in the Perl below.
        When I run it with two spreasheets open, sadly I do not get anything printed out.
        It seems that the in does not find anything.
        Where will I find more about 'in'.
        use OLE; use Win32::OLE::Const "Microsoft Excel"; use strict "vars"; use Win32::OLE 'in'; my $excel = Win32::OLE->CreateObject('Excel.Application'); for my $wb (in($excel->{Workbooks})) { print "in loop\n"; print $wb->{Filename},"\n"; }