Unload picture from a shape (not to delete the shape)

YasserKhalil
PlatinumLounger
Posts: 4914
Joined: 31 Aug 2016, 09:02

Unload picture from a shape (not to delete the shape)

Post by YasserKhalil »

Hello everyone
I have a code that I could insert a shape using such a line `ThisWorkbook.ActiveSheet.Shapes.AddShape` then loads a picture to the shape.
Is there a way to unload the image loaded into the shape without deleting it. All what I found is a way to delete the shape

Code: Select all

Public Sub UnloadPicture(ByVal sShapeName As String)
    Dim myShape As Shape
    On Error Resume Next
    Set myShape = ThisWorkbook.ActiveSheet.Shapes(sShapeName)
    If Not Err Then myShape.Delete
    Set myShape = Nothing
End Sub

Sub Test_UnloadPicture()
    UnloadPicture "MyImage"
End Sub
I could solve it using this part `If Not Err Then myShape.Fill.Solid` but I am not sure if there is a better way to unload the picture loaded on the shape or not?

User avatar
HansV
Administrator
Posts: 78499
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Unload picture from a shape (not to delete the shape)

Post by HansV »

I don't know of a better way.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4914
Joined: 31 Aug 2016, 09:02

Re: Unload picture from a shape (not to delete the shape)

Post by YasserKhalil »

When using this line `oPic.ShapeRange.LockAspectRatio = msoFalse` so as to fit the picture to specific cell, I noticed it is bigger a little than the cell.
Is there a way to make the picture a bit smaller?

User avatar
HansV
Administrator
Posts: 78499
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Unload picture from a shape (not to delete the shape)

Post by HansV »

Have you tried setting the Left, Top, Width and Height properties of oPic to those of the cell?
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4914
Joined: 31 Aug 2016, 09:02

Re: Unload picture from a shape (not to delete the shape)

Post by YasserKhalil »

Yes, I played around these properties and I could adjust them to suit my needs. Thanks a lot, my tutor.