Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Avoiding Win32::OLE errors

by esr (Scribe)
on Dec 09, 2004 at 04:53 UTC ( #413423=perlquestion: print w/replies, xml ) Need Help??
esr has asked for the wisdom of the Perl Monks concerning the following question:

I have a Perl script which creates/edits a Word document and then makes it visible for manual editing and possible printing. When the editing/printing has been done, the user closes the document and the Perl script detects this and continues with additional tasks. The code I use to detect that the document has been closed is as follows:
$mydoc = $word->Documents->Open($xfile); $mydocnt = $word->Documents->Count(); while (($mycnt = $word->Documents->Count()) == $mydocnt) { sleep 5; }
This mostly works fine but if the user closes Word rather than simply closing the file the above code produces an error which causes the Perl script to exit. The error messages say '"The RPC server is unavailable" in METHOD/PROPERTYGET "Documents"' and 'Can't call method "Count" on an undefined value' although the exact error messages are irrelevant for purposes of my question. The question is either: 1) How to detect when a document has been closed which also works if Word is closed instead? or 2) How do I prevent the obnoxious messages from terminating the Perl script?

Replies are listed 'Best First'.
Re: Avoiding Win32::OLE errors
by rupesh (Hermit) on Dec 09, 2004 at 05:21 UTC
Re: Avoiding Win32::OLE errors
by maa (Pilgrim) on Dec 09, 2004 at 07:47 UTC
    The MS Word VBA help files for this tells you why it's hard... the page is entitled "Using Events with the Application Object"
Re: Avoiding Win32::OLE errors
by esr (Scribe) on Dec 14, 2004 at 06:42 UTC
    I'm relatively new to PerlMonks so I wasn't sure whether I should respond to the others' comments on this subject or not. The suggestions offered here were interesting and led to some useful information but did not solve the problem. However I did find a hokey way around this specific case. In this situation it appears that closing Word causes the $word object to point to invalid stuff and that is what seems to be causing the error messages. I experimented with various combinations and found that the following code bypasses the problem of the Perl script being terminated although there is still a warning message issued. The code that seems to work for this case is:
    $icount = $word->Documents->Count(); $done = 0; while ($done == 0) { if ($word->Documents) { if (($mycnt = $word->Documents->Count()) != $icount) { $done = 1; } } else { undef $word; $word = Win32::OLE->GetActiveObjectord.Application') || Win32::OLE->new('Word.Application'); $done = 1; } sleep 5; }
    As I say, it's hokey but it seems to work for this specific case.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://413423]
Approved by BrowserUk
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2017-05-25 21:25 GMT
Find Nodes?
    Voting Booth?