I have used Win32::OLE to do some parsing of MS Word files. I've been unsuccessful in my attempt to the paragraph numbers from the source file.

The following is a likely example in Visual Basic, but I've not been able to successfully translate it into Perl.

If Selection.Paragraphs(1).Range.ListParagraphs.Count = 1 Then MsgBox Selection.Paragraphs(1).Range.ListFormat.ListLevelNumber Else MsgBox "Not a numbered paragraph" End If

The following is a snipit of my working OLE MS Word access:

sub doc2pt { my ( $docfile, $docpt, # Converted text file ) = @_; require Win32::OLE; require Win32::OLE::Enum; # NOTE: Win32::OLE appears to need + abs path my $document = Win32::OLE -> GetObject(abs_path($docfile)); die "Can't GetObject($docfile) $!\n" if !defined($document); print "Extracting Text ...\n"; open DOCPT, ">$docpt" or die "Can't open docpt file $docpt: $!"; my $paragraphs = $document->Paragraphs(); my $enumerate = new Win32::OLE::Enum($paragraphs); while (defined($paragraph = $enumerate->Next())) { my $style = $paragraph->{Style}->{NameLocal}; print DOCPT "style==>$style\n"; ### my $range = $paragraph->{Range}; ### my $count = $range->ListParagraph(); ### if (defined($count)) { ### my $paranum = $range->ListFormatListLevelNumber(); ### print DOCPT "paranum==>$paranum\n"; ### } my $text = $paragraph->{Range}->{Text}; $text =~ s/[\n\r]//g; $text =~ s/\x0b/\n/g; print DOCPT "text==>$text\n"; } close DOCPT; }

I would greatly appreciate any suggestions on how to solve this problem.


In reply to Using OLE to find MS Word paragraph numbers 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.