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

Hi Monks

I am having trouble with OLE and inserting pictures into a word document. The problem lies in getting a picture into the document after a page break. The pictures insert easily onto the first page but I cannot get them onto the second page - probably due to my lack of knowledge in Object programming. I have also used code I have found on the web and do not claim to be the author of most of it ;-)

here is my code:

use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Word'; use Win32::OLE::Variant; my $cur_style = 'a'; my $cur_bookmark = 'a'; my $word = CreateObject Win32::OLE 'Word.Application' or die $!; $word->{'Visible'} = 1; my $document = $word->Documents->Add; my $selection = $word->Selection; text ($document, "Page 1"); enter ($document); my $picture = insert_picture($document, 'C:\dee-dwive\pic1.jpg', 0, 1, + 0, 250); enter ($document); $selection ->InsertBreak(wdPageBreak); enter ($document); text ($document, "Page 2"); enter ($document); $picture = insert_picture($document, 'C:\dee-dwive\pic2.jpg', 0, 1, 0, + 250); enter ($document); save_doc_as($document, 'C:\dee-dwive\picture.doc'); #close_doc($document); #$word->Quit; sub text { my $document = shift; my $text = shift; $document->ActiveWindow->Selection -> TypeText($text); } sub enter { my $document = shift; $document->ActiveWindow->Selection -> TypeParagraph; } sub insert_picture { my $document = shift; my $file = shift; my $left = shift; my $top = shift; my $width = shift; my $height = shift; my $picture = $document-> Shapes -> AddPicture ( $file, $left, $top, $width, $height, ); return $picture; } sub insert_page_break { my $document = shift; $document->ActiveWindow->Selection->{Range} -> InsertBreak(wdPageBre +ak); }

Thanks for your helpful advice in advance

2006-05-05 Retitled by GrandFather, as per Monastery guidelines
Original title: 'OLE Problem'

Replies are listed 'Best First'.
Re: MS word insert picture again
by marto (Cardinal) on May 04, 2006 at 08:54 UTC
    Hi Secode,

    There are two ways to find out exactly where you are having problems. The first is to fully understand what this code you have found is doing, which I will come to in a moment, the second is to examine the output you have generated. The latter approach seems easiest to discuss at the first because you have told us that your not as familiar with OLE stuff as you would like to be. Firstly run your code. The keen eye (or the investigative one:) ) can see that there are two images, but they are in exactly the same position. If your images are the same size (say each images is 200 x 200) then you will not be able to see them both :) Try simply dragging one of the images with the mouse and moving it out of the way.

    To illustrate this change your second picture insert to:
    $picture = insert_picture($document, 'C:\dee-dwive\pic2.jpg', 0, 1, 0, 500);
    Then run your code. Note the change in of values from 250 to 500. Your output should now have both images. This does not solve your problem, I believe you want to insert the second image on page 2 of this document. As an exercise I think you would be better learning how to do that from the documentation I have listed below.

    Regarding learning more about Win32::OLE check out Hints for Microsoft Office automation and Using OLE with Perl.

    Hope this helps.

    Martin
Re: MS word insert picture again
by vkon (Curate) on May 04, 2006 at 08:50 UTC
    Your problem because your constant wdPageBreak not treated as constant.

    Reread better on what I wrote to you at Re: MS word insert picture, or wait for my English to improve and then ask again...

Re: MS word insert picture again
by Secode (Novice) on May 04, 2006 at 09:31 UTC

    Hi - Sorry vkon I don't have enough knowedge to understand what you are showing me which could fustrate you ;-( Martin - the pictures insert them selves ontop of each other instead of the second picture landing on the second page at the same position as the 1st picture on page 1 - no matter what I set the value to it does not move beyond the 1st page.
    Thanks for your comments

      Secode,

      Firstly, you have replied to your own post, not to either of the replies. This means that neither vkon or myself get a message to say that you have posted a reply. Secondly did you read any of the links I pointed you to in my post? I was pointing out where you went wrong, did you look at the documentation which explains how to drive this kind of application via Win32::OLE? You tell us that you do not understand what vkon is showing you, did you understand the example he posted in reply to your previous question MS word insert picture? I don't see any replies saying you did not understand what you were told. IMHO it would be a better investment of your time if you read the documentation and learn how work with the object model in question. Take a look at The Office object model which provides some useful links and code you may learn from. Taking code you have found on the web is really only worth while if you spend time understanding what it is doing and how it works, otherwise, as we see here, it is difficult to change it to do exactly what you want.

      In addition please read the PerlMonks FAQ and How do I post a question effectively? if you have not already done so.

      Martin