Is it possible to insert an image into an excel cell via COM?

I used automation to insert values ​​into a cell, however I have never seen any documentation, for example, which demonstrates the insertion of nothing but text and / or formulas.

Has anyone been able to insert an image from an external application?

+1
source share
3 answers
Dim FileName as string FileName="c:\text.jpg" Set NewPic = ActiveSheet.Pictures.Insert(FileName) NewPic.top=100 NewPic.left=100 

If you want to place the image in a specific cell, select this cell as a range and use the up / left / s ranges to do this.

Samples: http://exceltip.com/st/Insert_pictures_using_VBA_in_Microsoft_Excel/486.html

Note Excel cells cannot contain images. Pictures live on an invisible drawing layer that floats around the cells. They can be located based on the coordinates of the cell, which makes you feel that they live "in" the cells.

+2
source

I see that this has already been answered, but see my post here .

Basically, instead of using the Worksheet.Pictures.Insert method (which MSDN recommends you not to use directly and which returns a raw COM object), try using the Worksheet.Shapes.AddPicture method instead.

 Dim range As Microsoft.Office.Interop.Excel.Range Dim pic as Microsoft.Office.Interop.Excel.Shape Dim filePath as String range = ... filePath = ... pic = range.Worksheet.Shapes.AddPicture(filePath, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, range.Left, range.Top, 300, 300) 

It’s not so simple, because you need to specify the exact location and dimensions, but otherwise, pretty cool!

+2
source

Of course, the following code provides a good example using Microsoft Interop libraries:

  string excelfilename = @"C:\excelfile.xlsx"; string picturename = @"C:\image.jpg"; object missing = Type.Missing; Microsoft.Office.Interop.Excel.Application app = new ApplicationClass(); Workbook book = app.Workbooks.Add(missing); Worksheet sheet = (Worksheet)book.ActiveSheet; Pictures pics = (Pictures)sheet.Pictures(missing); pics.Insert(picturename, missing); book.SaveAs(excelfilename, missing, missing, missing, missing, missing, XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing); app.Quit(); app = null; 
+1
source

Source: https://habr.com/ru/post/897292/


All Articles