Title: Re: Win32::OLE -- Word 97 not visible on first try

(Please excuse the MIME and HTML garbage; our mail server is misconfigured)

Robert Coney wrote:
> On first execution (listed below), the "Now starting Word" message is
> printed, and according to Task Manager, Winword.exe starts,
> but it is not
> visible.  On second execution, Word becomes visible and
> everything seems
> OK.
>
> If Word is already active, this script works OK on the first try.
>
> What's wrong?
>
> use Win32::OLE;
> use Win32::OLE::Const 'Microsoft Word';
> my $classid = 'Word.Application';
> my $word = Win32::OLE->GetActiveObject( $classid );
> if ( ! $word )
>    {
>    print "No WORD app is active. Now starting Word...\n";
>    my($word) = Win32::OLE -> new($classid) || die 'Could not

This line is wrong. If there is no active Word instance, you create a new $word variable which receives the handle(?) to a new instance of Word. This variable's scope is the curly braces of the if. So when you leave the if, this variable goes out of scope.

> start Word';
>    my($doc) = $word -> Documents -> Add() || die 'New Document option
> failed';
>    }
> #
> $word -> {Visible} = 1; 

This always refers to the $word which was assigned with GetActiveObject, since the $word that is assigned to with new in the if() has gone out of scope.

Solution: get rid of the 'my($word)' inside the if and replace with plain '$word', like so:

   $word = Win32::OLE -> new($classid) || die 'Could not start Word';

Cheers,
Philip

Reply via email to