in reply to Style problem with Word/OLE

Sorry, I thought that the concise form would be easier to follow. Here is the full code:
use strict; use warnings; use Win32::OLE; use Win32::OLE::Const 'Microsoft Word'; use Cwd 'abs_path'; my $book = shift; if ( !$book ) { die "usage: $0 <word file>"; } $book = abs_path( $book ); my $word = Win32::OLE->GetActiveObject('Word.Application') || Win32::O +LE->new('Word.Application'); $word->{'Visible'} = 0; $word->{DisplayAlerts} = 1; my $file = $word->Documents->Open( $book ); if ( !$file ) { die "cannot open $book"; } my $paragraphs = $file->Paragraphs(); my $n = $paragraphs->Count(); for my $p (1..$n) { my $paragraph = $paragraphs->Item( $p ); print "paragraph $p\n"; print "before: $paragraph->{Style}->{NameLocal}\n"; my $text = $paragraph->{Range}->{Text}; $paragraph->{Range}->{Text} = $text; print "after: $paragraph->{Style}->{NameLocal}\n\n"; } $file->Close();
Output: paragraph 1 before: Heading 1 after: Heading 2 paragraph 2 before: Heading 2 after: Heading 3 paragraph 3 before: Heading 3 after: Normal paragraph 4 before: Normal after: Normal

Replies are listed 'Best First'.
Re^2: Style problem with Word/OLE
by ww (Archbishop) on Jul 06, 2013 at 20:08 UTC
    Concise is nice; relevant info is better. The Guidance here is to boil your problematic code down to a minimum (preferably not more than 10 -15 lines) that demonstrates the problem.

    Your latest code -- the non-concise form -- gives me an error message; if the same holds true for you, yyou should include that (and any warnings) verbatim, in your post to make it easier for us to help.

    That said, here's the way my attempt to run your new code went:

    Directory of D:\_wo_sch # Establishing existance of the file with whi +ch I tested: 06/28/2013 01:16 PM 56,320 ZBA7-8-13agenda D:\_wo_sch>D:\_Perl_\PMonks\1042932.pl D:\_wo_sch\ZBA7-8-13agenda.doc Undefined subroutine &main::abs_path called at D:\_Perl_\PMonks\104293 +2.pl line 14.

    So, in a perhaps excessively cursory manner (I'm running out of daylight here for outdoor tasks to be done in the coolth of the evening), I hunted about for a clue about that. Didn't find it (per se), but did find a couple resources which may help you: Re: Using OLE to view given Paragraph in MS Word Document (in Using OLE to view given Paragraph in MS Word Document) and Win32::OLE.

    And that leads to another tip. Super Search and Tia Google ( site: www.perlmonks.com <search term(s)> ) can be an source of solace, education and perhaps even solutions and should be resources to which the puzzled resort, early.

    And, of course, buried deep (...DEEP!) in perldoc Win32::OLE you'll find another clue.


    If you didn't program your executable by toggling in binary, it wasn't really programming!

      Sorry again, I missed a line. Now the code above is complete and tested. The exact output follows.

      Thanks for the pointers.

Re^2: Style problem with Word/OLE
by poj (Abbot) on Jul 07, 2013 at 20:10 UTC

    I tried numerous ways but the only one I found that preserved the style was to store it on a first pass and re-apply it on a second pass like this.

    my $paragraphs = $file->Paragraphs(); my $n = $paragraphs->Count(); my @style=(); # add for my $p (1..$n) { my $paragraph = $paragraphs->Item( $p ); print "paragraph $p\n"; print "before: $paragraph->{Style}->{NameLocal}\n"; $style[$p] = $paragraph->{Style}->{NameLocal}; # add my $text = $paragraph->{Range}->{Text}; $paragraph->{Range}->{Text} = $text; } for my $p (1..$n) { my $paragraph = $paragraphs->Item( $p ); $paragraph->{Style} = $style[$p]; # add print "after: $paragraph->{Style}->{NameLocal}\n"; }
    poj
      Thank you so much, Poj.

      Strangely, I tried a similar solution - saving and restoring the style in the same loop - and it didn't work.

      Your solution with two loops works well.

      Ilan