Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Warning - untested code follows! :-)

My usual approach when trying to find out how to automate something I am unfamiliar with in Excel is to record a macro, and I'm afraid that what I did confirmed my expectations. A picture is not related to a cell, but to a sheet. Therefore, all your references to cells, and your attempt to position the pictures relative to cells, are doomed to failure. You will probably have realised that your line $excel -> ActiveSheet -> Pictures -> Insert($image_file_full); # Insert in active cell makes no reference to the active cell, only the active sheet. However, I believe, based on other peoples' files including pictures that I have worked on, that inserting and deleting rows and columns will move and resize pictures.

Anyway, I recorded a macro and put two pictures into a sheet. The first thing that happened was, as you described, that the pictures went one on top of the other. So I dragged one of them down. The resulting VBA macro was:
ActiveSheet.Pictures.Insert( _ "C:\Documents and Settings\All Users\Documents\My Pictures\Sam +ple Pictures\Blue hills.jpg" _ ).Select ActiveSheet.Pictures.Insert( _ "C:\Documents and Settings\All Users\Documents\My Pictures\Sam +ple Pictures\Sunset.jpg" _ ).Select Selection.ShapeRange.IncrementTop 290.25
Although VBA litters recorded macros with "Activate" and "Select", it's usually bad practice to let these leak into production code, as they slow the system down without doing anything useful. It's OK to use them to get the user where you want him when you finish, and there are certain situations where they can't be avoided. But if I were trying to put two pictures into an Excel sheet, my VBA would look like the following untested code:
Dim picCurrent As Picture Dim shtPics As Worksheet Set shtPics = ThisWorkbook.Sheets(1) Const ksPic1 As String = "C:\Documents and Settings\All Users\Document +s\My Pictures\Sample Pictures\Blue hills.jpg" Const ksPic2 As String = "C:\Documents and Settings\All Users\Document +s\My Pictures\Sample Pictures\Sunset.jpg" Set picCurrent = shtPics.Pictures.Insert(ksPic1) Set picCurrent = shtPics.Pictures.Insert(ksPic2) picCurrent.Top = 130 picCurrent.Left = 280
From this, my first pass at Perl would be:
use strict; use warnings; use diagnostics; use Win32::OLE; my $xl = Win32::OLE->new('Excel.Application'); $xl->{Visible} = 1; my $wb = $xl->ActiveWorkbook; my $shtPics = $wb->Sheets(1); my $picCurrent = $shtPics->Pictures->Insert(first pic); $picCurrent = $shtPics->Pictures->Insert(second pic); $picCurrent->Top = 130; $picCurrent->Left = 280;
... and then I would start debugging.

HTH,

John Davies

In reply to Re: Adding pictures to Excel by davies
in thread Adding pictures to Excel by merrymonk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-19 19:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found