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!


In reply to Re: Using OLE to view given Paragraph in MS Word Document by ricDeez
in thread Using OLE to view given Paragraph in MS Word Document by Ray Smith

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.