Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6149443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:27:06+00:00 2026-05-23T19:27:06+00:00

I’m trying to open a word document and just extract all the text that

  • 0

I’m trying to open a word document and just extract all the text that is in the document and display it to the user using Win32::OLE

#usr/bin/perl
#OLEWord.pl

#Use string and print warnings
use strict;use warnings;
#Using OLE + OLE constants for Variants and OLE enumeration for Enumerations
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Word';

$Win32::OLE::Warn = 3;

#set the file to be opened
my $file = '/work/Test.docx';

#Create a new instance of Win32::OLE for the Word application, die if could not open the application
my $MSWord = Win32::OLE->new('Word.Application','Quit') and "Opened Word" or die     "Unable to open document ", Win32::OLE->LastError(); 
#Set the screen to Visible, so that you can see what is going on
$MSWord->{'Visible'} = 1;
#open the request file or die and print warning message
my $Doc = $MSWord->Documents->Open('C:\work\Test.docx') or die "Could not open ", $file, " Error:", Win32::OLE->LastError();

#$MSWord->ActiveDocument->SaveAs({Filename => 'AlteredTest.docx', 
                            #FileFormat => wdFormatDocument});
                            
                            
sub ShowObjs {
my $obj = shift;
foreach (sort keys %$obj) {
print "Keys: $_ - $obj->{$_}\n"; }
 }

 my $paragraphs = $Doc->Paragraphs;
 ShowObjs($paragraphs);

 #  Get and print the Text inside the opened file
 my $paragraphs = $Doc->Paragraphs;
 my $txt = $Doc->Range->Text;
 print $txt;
                            
 $MSWord->ActiveDocument->Close;
 $MSWord->Quit;

I’m getting this error code:

OLE exception from "Microsoft Word":

Command Failed

Win32::OLE(0.1709) error ox800a1066
in METHOD/PROPERTYGET "Open" at OLEWord.pl line 20

Update: I can open up the Word application fine, it’s just when I try to open up the file that is the problem

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:27:07+00:00Added an answer on May 23, 2026 at 7:27 pm

    I have few scripts to convert DOC to various output format using Win32::OLE. They usually start like this:

    use Win32::OLE;
    use Win32::OLE::Const 'Microsoft Word';
    my $wr = Win32::OLE->new('Word.Application')
        or die "Failure - word. \n";
    
    $wr->{DisplayAlerts} = wdAlertsNone;
    $wr->{Visible} = 0;
    
    my $Doc = $wr->Documents->Open({
        FileName           => $input_file_path,
        ConfirmConversions => 0,
        AddToRecentFiles   => 0,
        Revert             => 0,
        ReadOnly           => 1,
        OpenAndRepair      => 0,
    }) or exit;
    
    ...
    

    Please note that $input_file_path has to contain absolute path to your file. You can also enable Visible and DisplayAlerts to see any error Word might give you.

    Edit: You can traverse paragraphs using in enumerator:

    use Win32::OLE qw(in);
    
    ...
    
    my $paragraphs = $Doc->Paragraphs;
    for my $par (in $paragraphs) {
        print $par->Range->Text();
    }
    

    Or you can use Word’s own exporting method and save document as one of supported formats:

    $Doc->SaveAs({
        FileName   => 'c:\\work\\Test.txt',
        FileFormat => wdFormatEncodedText,
    });
    

    The advantage of latter method is that formatting is retained if possible, which produces better results for bullets, numbering and such.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.