in reply to Checking when Excel editing is done

Hi, esr. The general way of trapping error conditions in Perl, which I assume works in Win32::OLE programming as well, is eval blocks. So I would replace your call to $excel->ActiveWorkbook with a subroutine that looked something like (untested):

sub isWorkbookActive { my ($excel) = @_; eval { return $excel->ActiveWorkbook; }; return 1 if $@ =~ /message filter/; }

But frankly, you will more likely find a more specific (and less Perl-dependent) solution at the MSDN site (look here for starters). Any sample code you find will probably be in VB, but it should translate easily enough. Also look in the section of Win32::OLE's POD about Office. Update: Apparently the answer is more complex than that. Did you see the reply to one of your recent nodes that pointed to Trapping errors with Win32::OLE?

As a total aside, I must comment, esr, on your choice of username. I probably wasn't the only Monk who thought for a moment that you might be ESR of OSI/Cathedral and the Bazaar/fetchmail fame. It seems rather unlikely, given that the other ESR is neither a Win32 programmer nor an especially great fan of Perl, but it's still amusing. Update: ESR is not an opponent of Perl either, by any means. His full assessment is here.

Replies are listed 'Best First'.
Re^2: Checking when Excel editing is done
by esr (Scribe) on Dec 15, 2004 at 16:57 UTC
    Further experimentation indicates that Win32::OLE works differently and does not set $@, nor does the eval make any difference. But error messages can be obtained by using
    $errmsg = Win32::OLE->LastError;
    This can be searched for the needed string and then ignored. The message still comes out on the screen for the Perl script window but I found that can be turned off by setting the Warn level to 1 instead of 2. So a code snippet that seems to do the job is:
    $warnlevel = $Win32::OLE::Warn; $Win32::OLE::Warn=1; $done = 0; while ($done == 0) { if ($excel->ActiveWorkbook) { sleep 15; } else { $errmsg = Win32::OLE->LastError; if ($errmsg =~ m/busy/) { sleep 15; } else { $done = 1; } } } $Win32::OLE::Warn=$warnlevel;