Time to showoff your VBA skills – Help me fix ActiveSheet.Pictures.Insert snafu

Posted on July 30th, 2009 in VBA Macros - 25 comments

Okay, I am trying to build something hush hush super cool using excel but hit a roadblock trying to load images from a URL to my excel sheet.

What is the problem?

I am trying to insert an image from a URL to my excel 2007 workbook using VBA. The code I have used is,

Dim pPath
pPath = range("picLocation").value
ActiveSheet.Pictures.Insert(pPath)

insert-pictures-using-vbaWhen I try to run it, the error is “Unable to get the Insert property of the Pictures class”
Now, after googling I bumped on this piece of code recommended to fellow VBA coders who had the similar problem.

With ActiveSheet.Pictures.Insert(pPath)
.Left = range("a1").Left
.Top = range("a1").Top
End With

Well, I tried that, but still the same error. After spending what seemed like a couple of hours I am turning to you.

Do you know how to insert images (from URLs) to excel workbook using VBA?

Paste your code in comments. Lots of love and admiration is awaiting…

| More
Subscribe for PHD Email updates and get a free excel e-book with 95 tips & tricks

Comments
shokks July 30, 2009

I tried your code with 2003, it works.

But, I know Addpicture does not take URLs anymore with 2007 onwards, perhaps its the same with picture.insert as well.

http://support.microsoft.com/kb/928983/en-us

The above link gives the solution as “picture fill in a shape such as a rectangle”.

Vince E. July 30, 2009

Tried to recreate this, but it worked fine for me. I just took the image of the error you showed in the post. Is there more info that can narrow this down a bit?

Jane Moch July 30, 2009
Ian Hinckley July 30, 2009

Hi

Not sure if this is what you’re after, but I just tried this

Sub Macro1()
ActiveSheet.Pictures.Insert(”http://www.google.co.uk/intl/en_uk/images/logo.gif”).Select
End Sub

Tied a button to it on the sheet and it seems to work; hope this helps a little

Ian

Chandoo July 30, 2009

@All.. the issue is in Excel 2007. In 2003 ActiveSheet.Pictures.Insert seems to work fine. Unfortunately, I have design this in Excel 2007.. that is why I posted it here..

Ian Hinckley July 30, 2009

v2

Sub Macro1()
Set n = ActiveSheet.Pictures.Insert(”http://www.google.co.uk/intl/en_uk/images/logo.gif”)
With Range(”c12″)
t = .Top
l = .Left
End With
With n
.Top = t
.Left = l
End With
End Sub

Ian

Ian Hinckley July 30, 2009

That didn’t come out very well. This positions at c12, so can change easily:
Sub Macro1()
Set n = ActiveSheet.Pictures.Insert(”http://www.google.co.uk/intl/en_uk/images/logo.gif”)
With Range(”c12″)
t = .Top
l = .Left
End With
With n
.Top = t
.Left = l
End With
End Sub

Works OK in 2007

Ian

Mike Alexander July 30, 2009

Chandoo:
Try ‘ActiveSheet.Pictures.Insert’

With ActiveSheet.Pictures.Insert(”C:\Example.png”)
.Left = ActiveSheet.Range(”A1″).Left
.Top = ActiveSheet.Range(”A1″).Top
End With

Jon Peltier July 30, 2009

activesheet.pictures.insert “C:\Documents and Settings\Jon Peltier\Desktop\2007 stuff\insert_charts_2007.png”

Works for me in 2003 SP3 and in 2007 SP2.

Check the URL, and make sure you have internet connectivity.

What also works, and is newer (pictures.insert was supposedly deprecated in ‘97):

activesheet.shapes.addpicture “C:\Documents and Settings\Jon Peltier\Desktop\2007 stuff\insert_charts_2007.png”, false, true, 200,200,100,100

Unfortunately you must specify dimensions (the last four arguments) and you don’t necessarily know them. But the picture size is still related back to the original picture size, so you could use scaleheight and scalewidth to fix this.

Mike Alexander July 30, 2009

Chandoo: I just re-read your post.

The code I posted works for me. However, I’m using a local picture. If you try to add a picture from the web, this won’t work.

I remember solving this problem before by adding a rectangle shape first, then using the Shapes.AddPicture method to get a picture from the web.

I’ll find that code and post it here.

Chandoo July 30, 2009

Some more updates… The code “ActiveSheet.Pictures.Insert (path)” works fine in Excel 2007 at home. Strange it failed miserably on my work laptop. Do you think this has got something to do with SP2 of MS Office 2007 or something like that?

@Ian, Jon: Thanks for the code snippets. I guess I will use my home installation of excel to do this.

Mike Alexander July 30, 2009

Chandoo:

Try this on your work laptop:

Sub test()
ActiveSheet.Shapes.AddShape msoShapeRectangle, 50, 50, 100, 200
ActiveSheet.Shapes(1).Fill.UserPicture _
“http://www.datapigtechnologies.com/images/dpwithPig6.png”
End Sub

Jon Peltier July 30, 2009

I didn’t mean to post code with a local file, because both approaches worked with an internet image as well. This is in Excel 2007 SP2.

activesheet.pictures.insert “http://peltiertech.com/images/2009-07/col_area_noblanks.png”

Mike Alexander July 30, 2009

Jon: Looks like I have SP1 on my client machine! I wasn’t paying attention.

Just checked my home computer where I have SP2, and you’re right…looks like they fixed it.

Jon Peltier July 30, 2009

I didn’t even bother testing in SP1, though I could if anyone cares enough.

teylyn July 31, 2009

I’m afraid I don’t have a solution, but I find it remarkable that after attaining a certain status in the Excel world, Chandoo does not need to post on an Excel discussion forum to get help for an Excel problem. Instead, he posts on his blog and all the gurus come rushing to his help.

Isn’t Web 2.0 great?

Jon Peltier July 31, 2009

Teylyn – I saw Chandoo’s tweet first, and followed the link back to his blog.

Chandoo July 31, 2009

@Mike.. thank you. I have seen the fill rectangle solution before posting the query here. For that matter, I have also tried the solution of embedding a browser control on a spreadsheet. both of these seemed a bit extreme. That is why I have asked it here.

But I guess I will end up using it if I had to build this in work laptop.

@Teylyn: I have thought of posting this in a forum. (Unfortunately I have not been to any excel group in the last 5 years. Last time I was active was when I built a jave based excel sheet construction solution using POI.HSSF classes of Apache… ) After searching for a few hours, I found several forum posts where others had same problem and the solution recommended (using .left and .top parameters) is not working for me. Incidentally most of these solutions are from a certain Jon Peltier :P

I thought may be the problem is interesting for fellow blog readers. So I posted it here.

Justin B July 31, 2009

Hi,
Adapting the code in the question,

[code]
Sub InsPicture()
pPath = "http://chandoo.org/images/pointy-haired-dilbert-excel-charts-tips.png"
With ActiveSheet.Pictures.Insert(pPath)
.Left = Range("a1").Left
.Top = Range("a1").Top
End With
End Sub
[/code]

Seems to work fine

Jon Peltier July 31, 2009

Looks like it was a problem in 2007 up to SP1, which was corrected in SP2.

Chandoo July 31, 2009

@Jon.. seems like the case. I just checked the version at work laptop. it is 12.0.6331.5000 (SP1).

Thank you so much every one. I really appreciate your time and suggestions in solving this.

Jon Peltier July 31, 2009

Glad to help. I couldn’t understand why something so straightforward wasn’t working.

Kieranz August 14, 2009

Hi All
Is there a way of inserting a motion clip eg animated gif or swf or flv?
Thks

Chandoo August 14, 2009

You can insert animated GIFs by inserting them in a browser control through VBA. For other types of movies, I can guess you can insert them as clip art.

RSS feed for comments on this post. TrackBack URI

Leave a comment

   Name (required)

   E-mail (required, never displayed)

   URL


If you have a question, please ask in the forums

Recommended Excel, Charting, VBA books