Code to insert a picture, not a link

ner1277

New Member
Joined
Oct 4, 2022
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Slightly different problem than I saw in other threads: When I step through this code it put the image in the worksheet but gives me an error on the line in bold:

The error is object variable or with block variable not set. If It didn't get hung up on the error it would be fine. The goal is an image not linked so if i sent it to someone it works.

Sub xxx()

Dim fName As String
Dim pic1 As Object
Dim r As Range

filelocation = Range("path").Value & "\"
sheetstotal = Sheets.Count 'ADD
path = filelocation
file = Dir(path)
i = sheetstotal - 1 'EDIT
If Dir(filelocation) = "" Then Exit Sub

filelocation = path + file
Set r = ActiveCell

Set pic1 = Nothing
pic1 = ActiveSheet.Shapes.AddPicture(Filename:=filelocation, LinkToFile:=False, SaveWithDocument:=True, Left:=r.Left, Top:=r.Top, Width:=r.Width * 8, Height:=r.Height * 22)
pic1.Select

End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Since you're assigning an object to a variable, you'll need to use the keyword Set...

VBA Code:
Set pic1 = ActiveSheet.Shapes.AddPicture(Filename:=filelocation, LinkToFile:=False, SaveWithDocument:=True, Left:=r.Left, Top:=r.Top, Width:=r.Width * 8, Height:=r.Height * 22)

Hope this helps!
 
Upvote 0
I tried that and it didn’t work. I had it originally that way without the set pic1 = nothing and then I just said set pic1 = …. And it was the same result.

I also tried assigning pic1 as a picture and not an object. No difference. Any chance I’m missing a library or something weird like that.
 
Upvote 0
Can you confirm the exact error message when trying...

VBA Code:
Set pic1 = ActiveSheet.Shapes.AddPicture( . . . .)

???
 
Upvote 0
Solution
So I did what you said and I guess I was changing so many things that I messed something else up, so you were right, but I swear I had something really close and it didn't work. I guess close isn't good enough, thanks for the help. Final code is below and this did work.

Sub xxx()

Dim fName As String
Dim pic1 As Object
Dim r As Range

filelocation = Range("path").Value & "\"
sheetstotal = Sheets.Count 'ADD
path = filelocation
file = Dir(path)
i = sheetstotal - 1 'EDIT
If Dir(filelocation) = "" Then Exit Sub

filelocation = path + file
Set r = ActiveCell
Set pic1 = ActiveSheet.Shapes.AddPicture(Filename:=filelocation, LinkToFile:=False, SaveWithDocument:=True, Left:=0, Top:=0, Width:=r.Width * 3, Height:=r.Height * 33)
End Sub
 
Upvote 0
That's great, I'm glad you've got it sorted it.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top