I succeeded in filling a shape with an image. However, I loop through several images to create a report for each ranger. So once I created the report, I need to remove that image and fill the shape with another image. I have tried several things but just don't succeed in removing the previous image. Once the first image is loaded, even trying to just loop through and fill the shape again with another does not work. Once the first image is in it remains. I've even tried to load an image that is just a white block. That doesn't work either. Please help me remove this again
Sub rInsertAndResizePicture()
Dim PicturePath As String
Dim PictureName As String
Dim WhiteFill As String
Dim TargetCell As Range
Dim Pic As Object
Dim TargetShape As Shape
Dim MaxWidth As Double
Dim MaxHeight As Double
Dim ShapeWidth As Single
Dim ShapeHeight As Single
Dim shp As Shape
Dim imagePath As String, whitePath As String
Dim rngImage As Picture
' Specify the name of the shape
On Error Resume Next
Set TargetShape = rRep.Shapes("RangerImageSize")
On Error GoTo 0
' Set the path and file name
PicturePath = rSys.Range("Z2").Value
PictureName = rSys.Range("Z3").Value '"Joel Somseb Torra RR.jpg"
WhiteFill = "white-600X300.png"
imagePath = PicturePath & PictureName
whitePath = PicturePath & WhiteFill
' If the shape exists, add the image to it
If Not TargetShape Is Nothing Then
On Error Resume Next
TargetShape.Fill.UserPicture whitePath
' sRep.Pictures("rngImage").Delete
' Check if the shape contains an image
' If TargetShape.Type = msoAutoShape Then
' ' Delete the image from the shape
' TargetShape.Fill.ForeColor.RGB = RGB(255, 255, 255)
' End If
On Error GoTo 0
If Dir(imagePath) <> "" Then
Set rngImage = rRep.Pictures.Insert(imagePath)
' Set rngImage = imagePath
' Name the image as "rngImage"
rngImage.Name = "rngImage"
TargetShape.Fill.UserPicture imagePath
Else
TargetShape.Fill.UserPicture whitePath
' TargetShape.Fill.ForeColor.RGB = RGB(255, 255, 255)
End If
End If
End Sub
If you want to delete the picture, you have to apply the Fill.Solid method.
Something like this
TargetShape.Fill.Solid