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 = <>;

In reply to 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.