in reply to Re^2: how to convert this VB code to perl
in thread how to convert this VB code to perl

:-( Well...

... $obj_sel->InsertBreak({Type => 'PageBreak'}) ; doesn't look to me like a complete program... Can one assume that you've done all the things necessary to open the document you want to insert a page break into and then select the place at which to insert it ? It's not clear whether your issue is with driving OLE in general or with this method in particular.

Replies are listed 'Best First'.
Re^4: how to convert this VB code to perl
by proton-69 (Novice) on Jan 27, 2009 at 18:48 UTC
    Code goes like this
    #! c:\perl\bin\perl use strict; use warnings; use Win32::OLE; my $file = $ARGV[0]; my $image = $ARGV[1]; my $count = $ARGV[2]; my $class = "Word.Application"; my $word_ref = Win32::OLE->new($class) || die ("Failed to launch Word +due to", Win32::OLE::LastError()); $word_ref->{'Visible'} = 1; # Add the doc my $doc = $word_ref->Documents->Add; my ($obj_sel, $obj_shape); for (my $i=0; $i < $count; $i++) { $obj_sel = $word_ref->Selection; $obj_shape = $doc->Shapes; # add image $obj_shape->AddPicture($image); # add page break add_pagebreak($obj_sel); } # Save the file $word_ref->ActiveDocument->SaveAs($file); # destory objs undef $obj_sel; undef $obj_shape; $word_ref->Quit(); # destory contructor for word app undef $word_ref;
    I want to add $count number of images in doc file so that i need to put page break after each pic insert.

      Hmmm... the Word 2007 Developer Reference is extensive...

      After some experiments, I found that:

      # Add the doc my $doc = $word_ref->Documents->Add ; my $obj_sel = $word_ref->Selection ; for (1..$count) { $obj_sel->InlineShapes->AddPicture($image) ; $obj_sel->InsertBreak() ; }
      appeared to do the job.

      I discovered this by using the "Record macro" function in Word, doing the operations to insert a picture followed by a page-break, and then translating to Perl...

      ...the reference documentation will tell you what the things recorded in the macro are doing. But it's hard to discover what you need to do what you want -- which is the advantage of the record macro trick.

        Thanks, that worked :)

      Off the top of my head (I'm on Linux, not Windows), something like:

      $word_ref->Selection->EndKey(wdStory); $word_ref->Selection->InsertBreak(wdPageBreak);

      I'm sure you could super search or use Google to find an example, or check the link you have been given in the first reply to this question regards the Microsoft DOM documentation.

      Martin