views:

85

answers:

1

My Delphi 6 program need to place an image on each row of my of Excel Sheet. I could insert a picture to a fixed position with something I read from another post.

procedure insertImages(ActiveSheet: OleVariant; ImageFilePath: String; ImageHeight, PictureTop, PictureLeft: Integer);
var
  Picture: OleVariant;
begin
    try
       Picture := ActiveSheet.Pictures.Insert(ImageFilePath);
       Picture.Width := ImageHeight * Picture.Width /Picture.Height;
       Picture.Height := ImageHeight;
       Picture.ShapeRange.Left := PictureLeft;
       Picture.ShapeRange.Top := PictureTop;
       Picture.Placement := xlMove;
    except
    end; //try
end; //insertImages;

The above code works fine, but I having trouble passing the PictureTop and PictureLeft parameter to make it so there is different image on the 2nd column of each row?

How can I get the Top and Left value for a specific cell? Or is there a better way to do this?

Please help.

+3  A: 

For example If you use;

ActiveSheet.Cells[2, 2].Select;
ActiveSheet.Pictures.Insert(ImageFileName);

then your picture's top equals top of Cell[2, 2] and picture's left equals left of Cell[2, 2]

SimaWB
Hi, just tried but somehow in my loop of ten records, all ten images happen to be inserted on the same spot of my worksheet overlapping each other. And is not even on the correct position of my first record. Don't know why. It looks like the "select" isn't working or the Pictures.Insert() is not inserting relative to the active cell.
Snackmoore
I've just tested and it worked as I expected...
SimaWB
@Snackmoore - you've removed the 'Picture.ShapeRange.Left/Top' statements from your procedure, right?
Sertac Akyuz
Yes I have. Don't know what went wrong. I tried to isolation problem by starting a new project doing nothing but this plus I used ActiveSheet.Cells[2,2].Select;ActiveSheet.Pictures.Insert(ImageFilePath); instead of soft coding the cell and it still won't work.Would it be a office version problem? I am using Office 2007, Delphi 6 on XP. Thanks a lot.
Snackmoore
I read on other forum that it is a bug in Office 2007 that the image always appear on B4. Anyway, I set the Picture.Top and .Left to ActiveSheet.Cells[Row, Column].Top and .Left and it seem to work.Thanks again.
Snackmoore