Gossamer Forum
Home : General : Perl Programming :

accessing Word from Win32::OLE fails, Excel or IE works

Quote Reply
accessing Word from Win32::OLE fails, Excel or IE works
I'm a long-time perl user (started in the 4.018 days) but all of my experience to this point has been on UNIX and Linux. I'm running into a problem when trying to use Win32::OLE on a Windows XP system, completely up to date with patches. I have ActiveState Perl 5.8.8.819 installed. The system I'm developing on has Microsoft Office 2003 Professional installed, and it has Office 2003 SP2 applied.

I'm trying to automate the process of extracting some text from a big batch of MS Word documents, and Google searches turned up a few examples. Most of them start with code along the lines of:

use Win32::OLE;
use Win32::OLE::Enum;

$document = Win32::OLE->GetObject("1517am.doc");


Or:

use warnings;
use Win32::OLE;
use Win32::OLE::Const;

my $wd = Win32::OLE::Const->Load("Microsoft Word 11.0 Object Library")
or die Win32::OLE->LastError();

foreach my $k (keys %$wd) {
printf "$k = %s\n", $wd->{$k};
}

my $word = Win32::OLE->new('Word.Application', 'Quit') or die Win32::OLE->LastError();
$word->{'Visible'} = 1;



In either case, whether I call GetObject with a MS Word doc file (that does exist, and it's not a path problem, I've tried with full path and double-backslashed path separators) or I call new() for Word.Application, I always get an OLE error:

Win32::OLE(0.1707) error 0x8007007e: "The specified module could not be found" at ...


If I call Win32::OLE->new for 'Excel.Application' or 'InternetExplorer.Application', the code works -- the application starts up as expected. It's only when I try do something related to Word that I get this error.

Likewise, if I try the GetObject variant, but this time I give it a foo.xls file, Excel starts up correctly.

I banged my head against the wall for a few hours trying to figure out what I was doing wrong before I realized that the code works if I call some other application or use some other document type, it only seems to fail for Word.

Anyone have any idea what the problem might be? I've done some searching using both the hex error code and the message text, and I haven't turned up much useful.

Any suggestions people have would be much appreciated.

Thanks,

Tim
Quote Reply
Re: [enchanter] accessing Word from Win32::OLE fails, Excel or IE works In reply to
Hi,

I used to use something like this:

Code:
my $Word = Win32::OLE->new('Word.Application', 'Quit') || die Win32::OLE->LastError();
my $doc = $Word->Documents->Open($path);

.. and then use Save() to save it as HTML (so it can be read, and then the content grabbed out).

Hope that helps.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!