Export Image to Excel

Hi all, I am working on export some images to MS Excel (windows), here is my code:

Dim excel as new ExcelApplication
excel.Workbooks.Add
excel.visible = true

dim h as integer = 40 //height of the row
dim w as integer = 25 //widht of the column
dim PicWidth as integer
dim PicHeight as integer

excel.Rows(“2:3”).RowHeight = h
excel.Columns(“A”).ColumnWidth = w

excel.Range(“A2”).select_
excel.ActiveSheet.Pictures.Insert(“c:\temp\gear.jpg”).select_
excel.ActiveSheet.Pictures.height = h
PicWidth = excel.ActiveSheet.Pictures.width
excel.ActiveSheet.Pictures.left = (w*5.5 - PicWidth) / 2

excel.Range(“A3”).select_
excel.ActiveSheet.Pictures.Insert(“c:\temp\test.jpg”).select_’
excel.ActiveSheet.Pictures.height = h
PicWidth = excel.ActiveSheet.Pictures.width //Exception code 0: Width method of Pictures class failed, (failed on “width”)
excel.ActiveSheet.Pictures.left = (w*5.5 - PicWidth) / 2

Exception err as OLEException
MsgBox err.message

It works if the app exports only 1 image, however, there comes an exception when I tried to export the second one. Anything missing?

Another question is : what is the relation of the image size (height and width) with the cell height and width? How can I place the image in the center of the cell (both horizontal and vertical)? I place the image horizontally by (w*5.5 - PicWidth) / 2, I figure out the value 5.5 is just by trial and error =.=!

.HorizonalAlignment = xlCenter
.VerticalAlignment = xlCenter

I think

[quote=260518:@Tony Lam]It works if the app exports only 1 image, however, there comes an exception when I tried to export the second one. Anything missing?

Another question is : what is the relation of the image size (height and width) with the cell height and width? How can I place the image in the center of the cell (both horizontal and vertical)? I place the image horizontally by (w*5.5 - PicWidth) / 2, I figure out the value 5.5 is just by trial and error =.=![/quote]

Hi Tony,

The short answer is that Excel cannot insert a picture into a cell … But … there is a way to fool Excel and getting the dimensions of the cell and the picture to be aligned in the cell to make it look like the picture is inside the cell.

Try this code and let me know what you think:

[code] Dim excel as new ExcelApplication

excel.Workbooks.Add
excel.visible = true

excel.ActiveSheet.Range(“A2”).select_
excel.ActiveSheet.Pictures.Insert(“c:\temp\gear.jpg”).Select_
//5.52 is the conversion from points to characters
//Adjust cell width to picture width
excel.Columns(“A:A”).ColumnWidth = (excel.ActiveSheet.Shapes(“Picture 1”).Width/5.52)
//Row height does not need the conversion of points to characters
excel.Rows(“2:2”).RowHeight = (excel.ActiveSheet.Shapes(“Picture 1”).Height*1)

//Repeat for a second picture in the cell below it
excel.ActiveSheet.Range(“A3”).select_
excel.ActiveSheet.Pictures.Insert(“c:\test\test.jpg”).Select_
excel.Columns(“A:A”).ColumnWidth = (excel.ActiveSheet.Shapes(“Picture 2”).Width/5.52)
excel.Rows(“3:3”).RowHeight = (excel.ActiveSheet.Shapes(“Picture 2”).Height*1)

Exception err as OLEException
MsgBox err.message[/code]

The value of 5.5 that you figured out by trial and error was really close to the conversion of points to characters, which is 5.52.

I am not sure why, but the row height does not need the conversion factor of 5.52, and is the direct factor of 1:1.

Hi Euguen,

I tried to use your code to run the app, it causes an exception:

Exception code 0: Unable to set the RowHeight property of the Range class, (failed on “RowHeight”)

on the line:

excel.Rows(“2:2”).RowHeight = (excel.ActiveSheet.Shapes(“Picture 1”).Height*1)

any ideas?

Hi Tony,

Here is the working program that you can download: ExcelImportJPG.zip

This works on Xojo 2016 r1, on my Windows 10 computer, with Excel 2016. If you have a different version, then I can try and create a version for you.

Thanks for letting me know about the Exception code.

Hi Euguen,

I downloaded you example, but I got the same exception on the same line. :frowning:

I am using Xojo 2016r1, Windows 7 , Excel 2013. Really much thanks for your help.

Hi Eugune,

With reference to your code, I modified my code a bit, and I successfully got the result that I want, the key is “Picture x”. Here are my codes:

Dim excel as new ExcelApplication
excel.Workbooks.Add
excel.visible = true

dim h as integer = 40
dim w as integer = 25
dim PicWidth as integer

excel.Rows(“2:3”).RowHeight = h
excel.Columns(“A”).ColumnWidth = w

excel.Range(“A2”).select_
excel.ActiveSheet.Pictures.Insert(“c:\temp\gear.jpg”).select_
excel.ActiveSheet.Pictures.height = h
PicWidth = excel.ActiveSheet.Pictures(“Picture 1”).width
excel.ActiveSheet.Pictures(“Picture 1”).left = (w*5.52 - PicWidth) / 2

excel.Range(“A3”).select_
excel.ActiveSheet.Pictures.Insert(“c:\temp\test.jpg”).select_
excel.ActiveSheet.Pictures.height = h
PicWidth = excel.ActiveSheet.Pictures(“Picture 2”).width
excel.ActiveSheet.Pictures(“Picture 2”).left = (w*5.52 - PicWidth) / 2

Exception err as OLEException
MsgBox err.message

Thanks a lot for your help!!!

This was a good team effort, and I am glad that you were able to figure it out.

Happy to help :slight_smile:

Hi Eugene,

i just create a xojo program to open an exciting xls and insert images to the first column on my windows and it look like it was working but then when i move the xls to another machine none of the images show up at all since it look like the insert picture is simply a linked image instead of embedded image.

is there a way to actually insert embeded image into the xls???

If you have Einhugur plugins try with excelwriter, is easy to insert pictures on Excel with various methods and sources and is multiplatform

i got the excelwriter and put in the e-crypt and typelib file and when i compile, i got error on

Dim writer as ExcelWriterWorkbook = ExcelWriterWorkbook.Create(f, ZStream.GetZipEngineHandle())

any idea why??

[quote=429331:@Richard Duke]i got the excelwriter and put in the e-crypt and typelib file and when i compile, i got error on

Dim writer as ExcelWriterWorkbook = ExcelWriterWorkbook.Create(f, ZStream.GetZipEngineHandle())

any idea why??[/quote]

What error do you get ?

that is the error… something about zstream

Yes, if the image pasted into the first cell, then it will be saved with the Excel file. It seems like the link to the picture was saved, which is why opening the file on another computer didnt work.

A way to check if this is working is to determine the size of the file when you save it. If the picture size is 3 megabytes and saving the excel file is now over 3 megabytes, then it worked. If the saved Excel file is 9 kilobytes, then only the link to the picture was saved.

Hi Eugene , i got it working with MBS XL Plugin