Add an image to an Excel spreadsheet (Views: 100)


I try to embed an image (jpg or bmp) into a spreadsheet. It will go in the top left of the spreadsheet - like a letterhead, sort of. I've seen the methods that use late binding, but the code I'm modifying use the early binding object TExcelApplication (the instance is called ExcelApplication1). The following doesn't compile:


I get the error "undeclared identifier: Pictures". Any suggestions?


If WS is your worksheet:

{ ... }
WS.Shapes.AddPicture('C:\Pictures\Small.Bmp', EmptyParam, EmptyParam, 10, 160,
  EmptyParam, EmptyParam);


{ ... }
  Pics: Excel2000.Pictures; {or whichever Excel}
  Pic: Excel2000.Picture;
  Pic: Excel2000.Shape;
  Left, Top: integer;
{ ... }
Pics := (WS.Pictures(EmptyParam, 0) as Pictures);
Pic := Pics.Insert('C:\Pictures\Small.Bmp', EmptyParam);
Pic.Top := WS.Range['D4', 'D4'].Top;
Pic.Left := WS.Range['D4', 'D4'].Left;
{ ... }

EmptyParam a special variant (declared in Variants.pas in D6+). However in later versions of Delphi some conversions cause problems. This should work:


{ ... }
WS.Shapes.AddPicture('H:\Pictures\Game\Hills.bmp', msoFalse, msoTrue, 10, 160, 100,

But you may have to use a TBitmap to find out how large the picture should be.

<< Back to main page