Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Style problem with Word/OLE

by Anonymous Monk
on Jul 06, 2013 at 09:05 UTC ( [id://1042874]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I would like to make changes to Word paragraphs using perl by reading the paragraph text, modifying it and writing it back. But even without changing anything, after writing back, the paragraph inherits the style of the subsequent paragraph. Below I demonstrate it with a file that contains 3 paragraphs with styles "Heading 1", "Heading 2" and "Heading 3". After writing back, the styles became "Heading 2" and "Heading 3" and "Normal". Any idea what am I doing wrong?

Thanks, Ilan

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"; } Output: paragraph 1 before: Heading 1 after: Heading 2 paragraph 2 before: Heading 2 after: Heading 3 paragraph 3 before: Heading 3 after: Normal

Replies are listed 'Best First'.
Re: Style problem with Word/OLE
by ww (Archbishop) on Jul 06, 2013 at 11:58 UTC
    1. Not showing us the actual code that produces your output
    2. Not showing us the error your (posted) code produces: Can't call method "Paragraphs" on an undefined value at 1042874.pl line 8. (Your line 1)
    3. Not using strict and warnings
    4. Not showing us how you're reading your M$ Word document

    There may well be be some more, but this Monk isn't inclined to try to crystal ball the kind of incomplete and misinformation you've provided.


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

Re: Style problem with Word/OLE
by IlanB (Initiate) on Jul 06, 2013 at 17:45 UTC
    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
      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.

      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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1042874]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-04-16 08:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found