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

Hello. I seek to extract a single line from a Microsoft Word document using OLE. Here is my code thus far (it's not much):
use warnings; use strict; use OLE; my $word = CreateObject OLE "Word.Application"; $word -> {Visible} = 1; my $document = $word -> Documents -> Open("C:/file_to_open.doc");
Like I said, it's not much. Now, I know how to extract the contents of the entire file using this line --
my $text = $document -> {Content} -> {Text};
-- But this doesn't do me much good. I instead want only the fourth line of the document. Can anyone help me? Thank you in advance.

Replies are listed 'Best First'.
Re: Extract Single Line of Text from Word Document Using OLE
by jdporter (Paladin) on Jun 22, 2005 at 19:08 UTC
    Here's how I did it. I recorded a macro in Word so as to highlight the 4th line, then took the resulting VB script and converted it to perl. Note that I made it open the doc in the background, i.e. not visible, and read-only; and I quit as soon as I'm done getting the data I wanted.
    use Win32::OLE; use strict; my $docfile = "C:/file_to_open.doc"; my $w = new Win32::OLE 'Word.Application' or die; $w->{'Visible'} = 0; my $doc = $w->Documents->Open($docfile,0,1,0); $w->Selection->MoveDown({ Unit=>5, Count=>3 }); $w->Selection->EndKey({ Unit=>5, Extend=>1 }); print $w->Selection->Text; $doc->Close(0); $w->Quit(0);
    This uses Win32::OLE rather than OLE as yours does; the interfaces look similar enough.