Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Win32::OLE->GetObject Fails on MS Word Document

by roho (Bishop)
on Dec 16, 2020 at 22:08 UTC ( [id://11125309]=perlquestion: print w/replies, xml ) Need Help??

roho has asked for the wisdom of the Perl Monks concerning the following question:

I am attempting to read an MS Word document. The test file contains the text: Line 1, (blank line), Line 2, (blank line). The call to "GetObject" returns undef and $! is empty.

I am running Strawberry Perl 5.24.1 on Windows 10 with Win32::OLE version 0.1712 (the latest version according to metacpan). This should be very straightforward, but I cannot see what I am missing here. TIA for any suggestions.

#!/usr/bin/perl use strict; use warnings; use Win32::OLE; use Win32::OLE::Enum; my $document = Win32::OLE->GetObject("C:\\tmpx\\test.docx") or die " +GetObject failed: $!\n"; my $paragraphs = $document->Paragraphs(); my $enumerate = new Win32::OLE::Enum($paragraphs); while(defined(my $paragraph = $enumerate->Next())) { my $text = $paragraph->{Range}->{Text}; $text =~ s/[\n\r]//g; $text =~ s/\x0b/\n/g; print "$text\n"; }

"It's not how hard you work, it's how much you get done."

Replies are listed 'Best First'.
Re: Win32::OLE->GetObject Fails on MS Word Document
by davies (Prior) on Dec 16, 2020 at 22:39 UTC

    Try something like:

    use strict; use warnings; use Win32::OLE; my $word = Win32::OLE->new('Word.Application'); $word->{Visible} = 1; my $filename = 'C:/tmpx/test.docx'; my $document = $word->Documents->Open($filename);

    GetObject is something I rarely use or recommend. It is trying to take control of an existing instance of word, which may be doing anything. Create your own instance and you know it's clean. There isn't an instance of an application called "C:\\tmpx\\test.docx" for Perl to grab (although there may be an instance of word that is using this file), so it fails quite correctly.

    Regards,

    John Davies

    Update: fixed typo in text

      Thank you davies. That works fine. Leaves me wondering what (if anything) GetObject is good for.

      "It's not how hard you work, it's how much you get done."

        If you really need to access a running instance, that's what GetObject does. It's very rare, but can be useful if you are doing weird things and can end up with invisible instances that you want to close down elegantly rather than through the task manager. It can also be useful if you need to do some manual work first and then let the code take over.

        Regards,

        John Davies

Re: Win32::OLE->GetObject Fails on MS Word Document
by GrandFather (Saint) on Dec 16, 2020 at 23:01 UTC

    Works fine for me with Perl V5.32.0 MSWin32-x64-multi-thread on Windows 10. Are you sure your file path is correct and the file isn't locked (opened by something else)? You could try checking that the file exists:

    ... my $docPath = 'D:\Delme~~\test.docx'; print "Found file '$docPath'\n" if -f $docPath; my $document = Win32::OLE->GetObject($docPath) or die "GetObject fai +led: $!\n"; ...
    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11125309]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-25 07:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found