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 =.=!
[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.
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???
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.