I currently successfully parse MS Word Documents, extracting paragraph style and text. However, I've been unsuccessful in displaying a Word Document, given a specified paragraph number.
My sample program, demonstrates my problem - I can "read through the Word document" using enumerate->Next() (I'd rather position directly but Skip() doesn't seem to work), and although it appears that I get to the desired paragraph, the display does not appear.
I see that Selection may be what I want but I can't figure how to make that work. I lack the VBA documentation. And when I see some samples, I have not been successful in translating them to Perl / OLE calls.
Thanks for your attention.
#!/usr/bin/perl -w
# Simple case to open MS Word Document and view Nth paragraph
use strict;
use warnings;
use Win32::OLE;
use Win32::OLE::Enum;
use Cwd qw(getcwd abs_path);
my $ParaNo = 10; # Default target paragraph
my $InFile = shift if @ARGV > 0; # Required file name
my $app_name = "Word.Application.8"; # Word's application name
my $app;
eval {$app = Win32::OLE->GetActiveObject($app_name)}; # Use instanc
+e if already running
die "Word ($app_name) is not installed" if $@;
if (!defined($app)) {
$app = Win32::OLE->new($app_name, sub {$_[0]->Quit;})
|| die "Could not connect to $app_name $!";
}
$app->{'Visible'} = 1;
my $abspath = abs_path($InFile); # Word appears to need absolute pa
+th
my $doc = $app->Documents()->Open({
FileName => $abspath,
ReadOnly => 0,
});
die "Can't open doc $abspath: $!" if !defined($doc);
my $paragraphs = $doc->Paragraphs();
my $enumerate = new Win32::OLE::Enum($paragraphs);
if (!defined($enumerate)) {
die "Can't get enumerate for $InFile";
}
my $paragraph;
for (my $i = 0; $i<$ParaNo; $i++) {
$paragraph = $enumerate->Next();
}
my $style = $paragraph->{Style}->{NameLocal};
my $text = $paragraph->{Range}->{Text};
print "style=$style text=$text\n";
print "Why doesn't the view show this location?\n";
print "ENTER to quit\n";
my $ans = <>;