This snippet here reads an open word document and is really useful if you want to have a "live" word retrieval. It retrieves whatever's typed in the document, as well as the document's filename and its properties.
I wrote this code to find out what the current user was doing with Word and to provide him with information related ot what he'd typed.
package readWord; use strict; use Win32::OLE qw(in with); require Exporter; use vars qw(@ISA @EXPORT); @ISA = qw(Exporter); @EXPORT = qw(readActiveDoc); sub readActiveDoc # this method returns the name of the active document and 2 hashes con +taining every single # word found in the document and all the document's properties. Hence +it is ready for use { if ( my $word = Win32::OLE->GetActiveObject('Word.Application'))# c +onnect to word document { if ($word->Documents->Count()) # makes sure at least one documen +t is open { # next line for debug only # print "\nSuccessfully connected to existing word"; my $docName = $word->ActiveDocument->Name; # retrieve name my $docProperties = $word->ActiveDocument->{BuiltInDocumentPr +operties}; my $wordRange = $word->ActiveDocument->Content; return ($docName,$docProperties,$wordRange); } } return (undef,undef,undef); # if code reaches this line then it has +n't reached the other return } # implying either the program's not open or there' +s no document.

Replies are listed 'Best First'.
How to include the snippet elsewhere
by Foggy Bottoms (Monk) on Jun 20, 2003 at 14:24 UTC
    Here's the bit of code you may need to include in your code to use the snippet I posted...
    sub readWordDoc { my $wordContent; my $wordTitle; my $wordProperties; ($wordTitle, $wordProperties, $wordContent) = readWord::readActiveD +oc(); if ( (defined $wordTitle) and (defined $wordProperties) and (defined $ +wordContent) ) { my $content=""; foreach my $palabre (in $wordContent->Words) # retrieves every s +ingle word and adds it to content { $content.= "\n".lc($palabre->{Text}); } foreach my $prop (in $wordProperties) # prints out every single +property { print "\n".$prop->name.":".$prop->Value; } } }
    Heureux qui, comme Ulysse, a fait un beau voyage
    Ou comme celui-là qui conquit la Toison,
    Et puis est retourné plein d'usage et raison,
    Vivre entre ses parents le reste de son âge!

    J. du Bellay, poëte angevin

Re: Reading data from an open Word Document
by Anonymous Monk on Dec 09, 2003 at 09:37 UTC
    Hai, How to open particular page of word document?.. Ex. if i want to open 10th page of Test.doc which is having 20 pages.