InserPictureInRange method for excel VBA

  • Thread starter Thread starter the excel-arator
  • Start date Start date
T

the excel-arator

I found a snippit of code on an excel help website which is supposed to
insert a picture into a range of cells. However, when the code is run, I get
a compile error saying the sub or function is not defined. Should the code
below work? And if not, does anyone have any ideas on how to insert a picture
into a specified range of cells? The only way I know of is the "addpicture"
method where you have to specify in points where the image is insert, and how
large to make the image.
Using cell references for sizing and placement would be alot easier! I'm new
to VBA, so simplified explanations would be appreciated!

Here's the code I found:

Sub TestInsertPictureInRange()
InsertPictureInRange "C:\FolderName\PictureFileName.gif", _
Range("B5:D10")
End Sub

Thanks for you help!
 
This is not a standard function of Excel

You can also use this

Sub Test()
Dim myPict As Picture
With ActiveSheet.Range("B5")
Set myPict = .Parent.Pictures.Insert("C:\FolderName\PictureFileName.gif")
myPict.Top = .Top
'myPict.Width = .Width
'myPict.Height = .Height
myPict.Left = .Left
myPict.Placement = xlMoveAndSize
End With
End Sub
 
That works well, thanks a lot!

Ron de Bruin said:
This is not a standard function of Excel

You can also use this

Sub Test()
Dim myPict As Picture
With ActiveSheet.Range("B5")
Set myPict = .Parent.Pictures.Insert("C:\FolderName\PictureFileName.gif")
myPict.Top = .Top
'myPict.Width = .Width
'myPict.Height = .Height
myPict.Left = .Left
myPict.Placement = xlMoveAndSize
End With
End Sub
 
ActiveSheet.Shapes.AddPicture
"http://www.google.com/intl/en_ALL/images/logo.gif", msoTrue, msoFalse, 0, 0,
10, 10

I used to use that to link images (not embed) in Excel 2003 spreadsheets. In
Excel 2003 you could use AddPicture and a URL in place of a local image
filename, and you could enable and disable linking and embedding. That
prevented your workbook from choking on embedded images.

With the advent of Excel 2007 AddPicture no longer accepts URLs in place of
local image file names. See:

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

Now I use the following code, which does not allow for enabling linking and
disabling embedding, that I am aware:

Dim MyPicture As Shape
Set MyPicture = ActiveSheet.Shapes.AddShape(msoShapeRectangle, 0, 0, 10, 10)
MyPicture.Fill.UserPicture "http://www.google.com/intl/en_ALL/images/logo.gif"

UserPicture can accept a URL in place of a local image file name, but I
haven't figured out how to enable linking and disable embedding when using
UserPicture.

Can anyone suggest a way to insert an image from either a local file name or
a URL and still enable linking and disable embedding?

Thanks for your help.
 
Thank you - I was looking for that code :)

This is not a standard function of Excel

You can also use this

Sub Test()
Dim myPict As Picture
With ActiveSheet.Range("B5")
Set myPict = .Parent.Pictures.Insert("C:\FolderName
\PictureFileName.gif")
myPict.Top = .Top
'myPict.Width = .Width
'myPict.Height = .Height
myPict.Left = .Left
myPict.Placement = xlMoveAndSize
End With
End Sub
 
Back
Top