1. Add my before $paragraph in while(defined($paragraph=$enumerate->Next())).
  2. Typo: Change ListParagraph to ListParagraphs.
  3. Typo: Change ListFormatListLevelNumber to ListFormat->ListLevelNumber.
  4. You might need ListFormat->ListValue instead of (or in addition to) ListFormat->ListLevelNumber.
  5. To match your VB code, check for $count->{Count} == 1 inside the if (defined $count) {...} block.

Working, tested code:

#!/usr/bin/perl use strict; use warnings; use Cwd qw( abs_path ); use Data::Dumper; doc2pt( "C:/a/perls/pm/PM_932635_data.doc" ); sub doc2pt { die if @_ != 1; my ( $doc_path ) = @_; ( my $out_path = $doc_path ) =~ s{\.doc$}{.out} or die "doc_path '$doc_path' does not end in '.doc'"; require Win32::OLE; require Win32::OLE::Enum; # NOTE: Win32::OLE appears to need abs path my $abs_doc_path = abs_path($doc_path); my $document = Win32::OLE->GetObject($abs_doc_path) or die "Can't GetObject($abs_doc_path) $!\n"; print "Extracting Text ...\n"; open my $out_fh, '>', $out_path or die "Can't open output file '$out_path': $!"; my $debug = sub { die if @_ != 2; my ( $name, $value ) = @_; local $Data::Dumper::Useqq = 1; local $Data::Dumper::Terse = 1; printf {$out_fh} "%-10s ==> %s", $name, Dumper $value; }; my $paragraphs = $document->Paragraphs(); my $enumerate = Win32::OLE::Enum->new($paragraphs); while ( my $paragraph = $enumerate->Next() ) { my $style = $paragraph->{Style}->{NameLocal}; $debug->( style => $style ); my $range = $paragraph->{Range}; my $count = $range->ListParagraphs(); if ( defined $count ) { my $real_count = $count->{Count}; my $paranum = $range->ListFormat->ListLevelNumber(); my $paraval = $range->ListFormat->ListValue(); $debug->( real_count => $real_count ); $debug->( paranum => $paranum ); $debug->( paraval => $paraval ); } my $text = $paragraph->{Range}->{Text}; $text =~ tr{\n\r}{}d; $text =~ tr{\x0b}{\n}; $debug->( text => $text ); } close $out_fh; }


In reply to Re: Using OLE to find MS Word paragraph numbers by Util
in thread 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.