Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Win32::OLE create object failing

by esr (Scribe)
on Sep 10, 2006 at 00:59 UTC ( #572186=perlquestion: print w/replies, xml ) Need Help??
esr has asked for the wisdom of the Perl Monks concerning the following question:

I have several perl programs which have been running successfully for some time on WinXP using WIN32::OLE, using the interface for both Excel and Word. These programs run on both of our home machines. Recently I started having problems creating the object for Word but only on one of the machines, not the other. The following snippet has worked correctly in the past and still works on the other machine but for some reason it is not returning the object reference.
$word = Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new('Word.Application'); $mydoc = $word->Documents->Open($xfile);
The Open fails because $word is not defined. Similar code for Excel still works fine.

The boilerplate at the beginning of the program contains:

use Win32; use Win32::OLE; use Win32::OLE "with"; use Win32::OLE::Const 'Microsoft Excel'; use Win32::OLE::Const 'Microsoft Word'; use Win32::OLE::Const 'Visual Basic For Applications'; use Win32::OLE::Variant;

It appears that something has changed on this machine but I don't know what to look for. I recently had some problems with my virus checking program for which the "fix" caused some registry problems for I.E. but AFAIK fixing those did not affect anything else.

I have also tried using CreateObject and GetObject but they do not return an object reference either. Is there something in the OLE stuff that gets registered that may have gotten unregistered? Any ideas of what I should look for?

Addendum: When I set Win32::OLE::Warn=2 I get the error message that I have an "Invalid Class String".

Replies are listed 'Best First'.
Re: Win32::OLE create object failing
by gellyfish (Monsignor) on Sep 10, 2006 at 07:59 UTC

    On determining that $word is undefined you should print the value of Win32::OLE->LastError() - something like:

    if (! $word ) { die Win32::OLE->LastError(),"\n"; }
    However from the warning that you are getting it does look like that all or part of the Word COM server is not registered. You can check this using the OLE Browser that comes with the Activestate Perl (or the one that comes with the Microsoft SDK if you have that,) or at a push you can compare the registry entries from a machine where this is working using regedit (i.e. search for Word.Application ). Probably the best way to repair this would be to re-install word, but you could re-register the COM server using regsrvr32 (i.e. something like regsrvr32 /i winword.exe but I don't have a windows machine to test it at the moment.)


      Thanks. A registry search on the two machines showed that there were several differences and there was a lot of Word stuff not registered. The "registry server" program did not accept the winword.exe input as suggested but I did discover an (undocumented) "startup switch" for Word.

      Entering "winword /r" in the "DOS prompt" (aka Run) window rebuilt the needed registry entries. The description of this switch says that it "Starts Word, starts Setup, makes changes in the Windows registry, and then quits. This switch forces a re-register of Word in the Windows registry."

      My programs are working again. :-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://572186]
Approved by Joost
[marto]: Corion Depeche mode were great last night
[Corion]: marto: Whooo! ;) That gives me hope for the large-stadium experience ;)

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (11)
As of 2017-03-27 11:20 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (319 votes). Check out past polls.