Search code examples
delphiexceldelphi-6vba

Insert an image on each row in an Excel Sheet with Delphi


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.


Solution

  • 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]