Using VBA to insert and keep images in file - Excel 2013

FlyingM picture FlyingM · Nov 11, 2013 · Viewed 37.5k times · Source

I'm working on a macro for a friend of mine who needs to import a set of images in an excel document and later use this document on other computers. The problem I encountered is that when opening this document on a different computer, all the images are gone and instead you get these little error signs, indicating that the image path could not be found.

I have developed the macro on my own computer where I have Excel 2007 and for me, the code works perfectly fine. My friend uses Excel 2013 and apparently, there seems to be a major difference on how those 2 versions deal with the image importing and saving.

Overall, I found 2 different ways how to insert images. The first one I tried was something similar to this:

Set pic = ActiveSheet.Pictures.Insert("C:\documents\somepicture.jpg")

The second way of doing this looked like this:

Set pic = Application.ActiveSheet.Shapes.AddPicture("C:\documents\somepicture.jpg", False, True, 1, 1, 1, 1)

In the documentation for this 2nd approach it is said that the 3rd paramenter (which is True here) is responsible for saving the picture with the document.

However, both these approaches look more or less the same in the end result: They work fine for me but won't work if they are executed on my friends pc with Excel 2013. So what I need is a work-around for the newer Excel versions (I read somewhere that from Excel 2010 upwards, there is a bug or something like that with these image import methods).

Answer

user2140261 picture user2140261 · Nov 11, 2013

In all my uses, Adding a picture with Insert makes a reference to a file on your harddrive, for whatever reason if you want the image to be embedded in the file you have to add a shape and then put the image on the shape using the AddPicture (like you use), I have never had any issues with this.

Also you are giving the picture a height and width of 1 pixel, You will almost never be able to see that true setting that higher as below:

Application.ActiveSheet.Shapes.AddPicture "C:\documents\somepicture.jpg", False, True, 1, 1, 100, 100

I have a feeling it was working all along and you just never saw the picture cause it was too small.