Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Word and Win32::OLE

by Bloodelf (Beadle)
on Sep 20, 2001 at 21:12 UTC ( #113635=perlquestion: print w/replies, xml ) Need Help??
Bloodelf has asked for the wisdom of the Perl Monks concerning the following question:

I am working on a script which needs to open a word (.doc) document, select all the text and then put the selected text into a variable. I can select the text, but have been unsuccessful in putting it into the variable. Here is what I have so far

use Win32::OLE; my $Word = Win32::OLE->new('Word.Application', 'Quit'); $Word->Documents->Open("C:/test/test.doc"); $Word->Selection->WholeStory;

Any ideas....?

Replies are listed 'Best First'.
Re: Word and Win32::OLE
by cacharbe (Curate) on Sep 20, 2001 at 22:03 UTC
    If you want the text, try this.
    use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Word'; $Win32::OLE::Warn = 3; # Die on Errors my $Word = Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new('Word.Application', 'Quit'); $Word->{'Visible'} = 1; my $infile = <SOME FILE PATH>; my $Doc = $Word->Documents->Open($infile); $Doc->Content->Select(); my $text = $Word->Selection->{Text}; print $text; print Win32::OLE->LastError();
    The application object is actually the best place to get the selection text from in this case, not the document object.

    Update: Although you can use the ->WholeStory; method that you used to select the whole document text and leave the creation of the document object alone, the Open method returns said object.


Re: Word and Win32::OLE
by CubicSpline (Friar) on Sep 20, 2001 at 21:56 UTC
    Are you sure that the method WholeStory is the one you want to call? The default property for the Selection object is Text. try
      Rereading your post, I think I might have jumped the gun. It looks like the WholeStory method selects the entire text of the document. So you'd still want to do that, but then try setting your variable with the Text property call as shown.
Re: Word and Win32::OLE (retrieving text from a word document)
by Foggy Bottoms (Monk) on Jun 16, 2003 at 15:55 UTC
    You can try this little script. It should work just fine.
    package readWord;
    use strict;
    use Win32::OLE qw(in with);
    require Exporter;
    use vars qw(@ISA @EXPORT);
    #use LTG::DocBuddy::Config qw($DEBUG); 
    @ISA    = qw(Exporter);
    @EXPORT = qw(readActiveDoc);
    sub readActiveDoc
    # this method returns the name of the active document and a hash containing every single
    # word found in the document. Hence it is ready for use
       if (my $word = Win32::OLE->GetActiveObject('Word.Application'))# connect to word document
          # next line for debug only
          # print "\nSuccessfully connected to existing word";   
          my $docName = $word->ActiveDocument->Name; # retrieve name
          my $wordRange = $word->ActiveDocument->Content; 
          return ($docName,$wordRange);
          return undef;

    The method returns undef if no document was found. It actually take currently open document but it's easy to change and actually make it another file. Please note that the document needn't be saved which actually makes this code even better.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://113635]
Approved by root
and the voices are still...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2018-03-20 22:25 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (260 votes). Check out past polls.