Cutting and pasting inserted image

mikeymay

Well-known Member
Joined
Jan 17, 2006
Messages
1,645
Office Version
  1. 365
Platform
  1. Windows
I am using the following in a loop to add 3 different images to a worksheet and then place in the correct location.

Multiple files are created but use only the same 3 images
Code:
Set objImage = ActiveSheet.Pictures.Insert(ActiveWorkbook.Path & "\Images\" & strImageFile)
            
objImage.Cut
            
rngInsert.Offset(-29, 2).Select
            
ActiveSheet.Paste
I have tried to add the image to the specified cell but sometimes it doesn't place it in the specified cell.

With the above code I am sometime getting an error when
Code:
ActiveSheet.Paste
tries to execute but if I F8 that line it executes with no problem. The error message that displays is 'Run-time error '1004': Paste method of Worksheet class failed'
I don't understand why this only happens sometimes and with different images that do paste correctly when utilised in previously created files within the loop.

TIA
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Maybe the error occurs in some cases because the image hasn't finished being copied to the clipboard before the pasting takes place. Try the following instead...

Code:
Set objImage = ActiveSheet.Pictures.Insert(ActiveWorkbook.Path & "\Images\" & strImageFile)

With rngInsert.Offset(-29, 2)
    objImage.Left = .Left
    objImage.Top = .Top
End With

If you're using the cut and paste method because of the bug that exists with Pctures.Insert, try using the AddPicture method of the Shapes object instead...

Code:
Dim objShape As Shape

Set objShape = Sheet1.Shapes.AddPicture(Filename:=ActiveWorkbook.Path & "\Images\" & strImageFile, _
    LinkToFile:=msoFalse, _
    SaveWithDocument:=msoTrue, _
    Left:=rngInsert.Offset(-29, 2).Left, _
    Top:=rngInsert.Offset(-29, 2).Top, _
    Width:=-1, _
    Height:=-1)

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
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