http://qs321.pair.com?node_id=819196


in reply to Adding pictures to Excel

You can add a picture so that the top-left of the image is in the top left of the active cell. You were actually very close in your attempt. I've tested that the following works:
use strict; use warnings; use OLE; use Win32::OLE::Const "Microsoft Excel"; my $excel = Win32::OLE->new('Excel.Application'); $excel->{Visible} = 1; my $workbook = $excel->Workbooks->Add; my $sheet = $workbook->Sheets(1); my $path = 'c:/images/'; $sheet->Range('a1')->Activate; $sheet->Pictures->Insert($path.'Img1.jpg'); # top-left of pic in activ +e cell $sheet->Range('f10')->Activate; $sheet->Pictures->Insert($path.'Img2.jpg'); # top-left of pic in activ +e cell

--
I'd like to be able to assign to an luser

Replies are listed 'Best First'.
Re^2: Adding pictures to Excel
by davies (Prior) on Jan 23, 2010 at 16:46 UTC
    In my experience, it's very rare to need to use the active cell for anything. But you're quite right, setting the active cell changes the import point. I, as Psmith would say, confused the unusual with the impossible. It's still ugly - I want my users to run my macros and end up back where they started. But I have Gargled extensively and found no way of putting a picture over a cell directly except by using the active cell. However, I have found an improvement on my previous code. Rather than the loops to work out the top and left of the target cell, the following works:
    $nVPos = $rTarget->Top; $nHPos = $rTarget->Left;
    And these can be refactored into the assignments so that the lines become something like:
    $picCurrent->{Top} = $rTarget->{Top}; $picCurrent->{Left} = $rTarget->{Left};
    Anchoring in this way means that inserting or deleting rows and columns moves the picture, so I think we now get the best of all worlds.

    Thanks, all - I've learned something new about the tool that pays my bills!

    John Davies
      Of course that's a good point that the starting location in the sheet is lost. I didn't consider that because if this is being done just to create reports in Excel (my normal use) then it really shouldn't matter as the user has never been in the sheet to begin with, but in the case of an existing sheet, it is an issue. I do like your idea of adjusting location after insertion, it seems neater than jumping around on the sheet and would only be improved if Excel would allow one to specify the insertion point to start with.

      I tested a bit and your method appears to have the same results as inserting in the active cell too, with respect to effects of row and column insertion/resizing shifting the cell later. I wondered if your method would associate the image with an absolute sheet location (which might even be handy in some cases), but it appears to link to the cell location, so all in all I think you have the winner!

      --
      I'd like to be able to assign to an luser

      Even better!! Great.
      I have tested more with this and confirmed that the pictures are placed where I wanted them to be.
Re^2: Adding pictures to Excel
by merrymonk (Hermit) on Jan 23, 2010 at 17:02 UTC
    Many thanks for this contribution.
Re^2: Adding pictures to Excel
by merrymonk (Hermit) on Jan 24, 2010 at 17:17 UTC
    Something odd has happended. I copied your Perl and inserted my path to the picture I wanted to use.
    On my system (Excel 2007 on Windows XP) the two pictures ended up on top of each other (and not near either of the target cells).
      That is odd. I tested both methods from this thread on my Win XP/Excel 2002 machine and they both work as expected. The only difference is that with davies' code above both images are inserted at the same point and you can see the second picture location change (but that won't be an issue when Excel visibility is turned off). With my version the images are inserted in the specified (different) places initially. Assuming your cell references in the code are not the same, I can't think of an explanation right now.

      --
      I'd like to be able to assign to an luser