http://www.perlmonks.org?node_id=939328


in reply to Using OLE to view given Paragraph in MS Word Document

I managed to get this to work with the changes made as per below:

#!/usr/bin/perl -w # Simple case to open MS Word Document and view Nth paragraph use strict; use warnings; use 5.012; 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 ##################################################################### # For the purposes of testing, I hard-coded the file name and path ##################################################################### my $InFile = "C:/Users/Ric/Desktop/Report-WirelessSurvey.doc"; ##################################################################### # The following makes the code less portable, requiring $app_name to # be modified accordingly! ##################################################################### # my $app_name = "Word.Application.8"; # Word's application nam +e # my $app; ##################################################################### # This approach will use the active instance or will open word if # required ##################################################################### my $doc = Win32::OLE->GetObject ( $InFile ) or die "Could not load $InFile. \n"; my $app = $doc->{Application}; $app->{Visible} = 1; ##################################################################### # This is a good idea ##################################################################### $app->{DisplayAlerts} = 0; # eval {$app = Win32::OLE->GetActiveObject($app_name)}; # Use insta +nce 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 +path # my $doc = $app->Documents()->Open({ # FileName => $abspath, # ReadOnly => 0, # }); # die "Can't open doc $abspath: $!" if !defined($doc); ##################################################################### # Why are you using Win32::OLE::Enum? ##################################################################### 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++) { for my $i ( 1 .. $paragraphs->Count()){ last if $i > $ParaNo; #Forgot that you wanted to stop here! $paragraph = $paragraphs->Item( $i ); ################################################################## # This bit needs to be in the loop! ################################################################## 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 = <>; # $paragraph = $enumerate->Next(); }

Try these changes and let me know how you go. This may still trip up on unicode characters!