How to insert an embedded picture?
Asked Answered
A

3

11

xlApp.activesheet.Pictures.Insert(strImagePath) inserts pictures into a spreadsheet as a linked picture. If I send the spreadsheet out of our network the images fail.

How can I place an image as an embedded image?

I am also calling this method from Access.

Amaro answered 14/6, 2013 at 14:6 Comment(0)
P
22

you can use the shapes.addpicture method

activesheet.Shapes.AddPicture Filename:="C:\test\desert.jpg", linktofile:=msoFalse, _
            savewithdocument:=msoCTrue, Left:=0, Top:=0, Width:=100, Height:=100
Pradeep answered 14/6, 2013 at 14:35 Comment(7)
Thanks @JosieP! This works great. Now I am trying to assign it to a Shape object so I can make some adjustments. I did Dim shpPic as Shape and Set shpPic = xlApp.activesheet.Shapes.AddPicture ... and I get a type mismatch error. Any idea why?Amaro
based on the xlApp part I guess you're automating from another app-does that app have Shape objects? if so use dim shpPic as Excel.ShapePradeep
Hi @JosieP, yes I'm calling from Access. I can't use Excel.Shape because I don't want to have a reference to the Excel library because I have users with a large range of systems. I declared my excel instance like this: Dim xlApp as Object Set xlApp = CreateObject("Excel.Application") So I tried Dim shpPic = xlApp.Shape and that gives me "User-defined type not defined" error. Any ideas?Amaro
you've gotta declare as Object if you're late binding excelPradeep
Oh ok, thanks. I'm not very familiar with late binding. So would I declare like this: Dim shpPic As Object Set shpPic = xlApp.Shape? I tried that and it said "Object doesn't support this property or method"Amaro
no you Dim shpPic as Object then use the set shpPic = xlapp.activesheet.shapes.addpicture...Pradeep
Note that you can set the required Width and Height parameters to -1, which then maintains the height and width of the original image!Waggoner
W
1

Note that you can set the required Width and Height parameters to -1, which then maintains the height and width of the original image!

Activesheet.Shapes.AddPicture Filename:="C:\image.jpg", LinkToFile:=msoFalse, _
        SaveWithDocument:=msoTrue, Left:=0, Top:=0, Width:=-1, Height:=-1

http://excelmatters.com/2013/11/25/default-picture-size-with-shapes-addpicture/

(Added as another answer to increase visibility as I've struggled with this problem for ages and haven't found this solution documented anywhere else.)

Waggoner answered 6/1, 2018 at 10:5 Comment(0)
M
-1
Activesheet.Shapes.AddPicture Filename:="C:\image.jpg", LinkToFile:=msoFalse, _
        SaveWithDocument:=msoTrue, Left:=0, Top:=0, Width:=-1, Height:=-1

this works, maybe the following code can help someone too (it helped me) this is how you select the image you've just added:

ActiveSheet.Shapes(ActiveSheet.Shapes.Count).Select
Megargee answered 10/6, 2019 at 10:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.