I have pictures (TBitMap) in cells of the TAdvStringGrid, and I want to copy this table to an Excel file using OLEVariant. Below I will paste only a few lines of code to introduce you with 2 ways I can paste one picture 'in' a particular cell of the Excel file from the particular TStringGrid cell:
// 1st way
Clipboard.Assign(StringGrid1.GetBitmap(2, 2));
Worksheet.Range['a1','a1'].Select;
Worksheet.Paste;
// 2nd way
bmp := StringGrid1.GetBitmap(2, 2);
bmp.SaveToFile('test.bmp');
Worksheet.Range['a1','a1'].Select;
Worksheet.Pictures.Insert('test.bmp');
I wrote 'in' using quotes because, in the resulting Excel sheet, pasted image is not really attached to a cell I used in code, that is, if I change height/width of row/column the cell related to, the picture will not follow it and/or change it size accordingly.
I googled that there are properties for pictures in Excel that can associate and lock them to a cell if set as True (ticked in the checklist in "Format picture" menu):
- move and size with cell
- print object
- locked.
Unfortunately, I couldn't find the way to access those properties using Delphi, only VBA examples. So if you know how to do that (even if a different ways of pasting or Excel document creation should be used), please, share, it will be highly appreciated.
Upd. 1. The VBA code I mentioned is:
Dim P As Object
Dim xlApp As Excel.Application
Dim WB As Workbook
Set xlApp = New Excel.Application
With xlApp
.Visible = False
.DisplayAlerts = False
End With
Set WB = xlApp.Workbooks.Open(FilePath, , True)
Set P = xlApp.Sheets(1).Pictures.Insert(PicPath)
With P
With .ShapeRange
.LockAspectRatio = msoFalse
.Width = 375
.Height = 260
End With
.Left = xlApp.Sheets(1).cells(y, x).Left
.Top = xlApp.Sheets(1).cells(y, x).Top
.Placement = 1
.PrintObject = True
End With
WB.SaveAs FileName:= NewName, CreateBackup:=False
WB.Close SaveChanges:=True
xlApp.DisplayAlerts = True
xlApp.Application.Quit
(taken from OP post here)