This is my PowerPoint file:
This is my code to change image of shape:
Dim presentation As Object Set ppt = CreateObject("PowerPoint.Application") Set presentation = ppt.Presentations.Open2007("D:18\temp.pptx", MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoTrue) Dim oSlide As Object Set oSlide = presentation.Slides(1) oSlide.Shapes("Picture").Fill.UserPicture ("C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg")
How can I change the image of the Shape object?
What you are trying to do is to create a fill, which doesn’t work because the shape in question is a picture. You can try it out yourself in PowerPoint. Setting a fill to a picture has no effect because the original image is still visible. That’s why you see no result.
You can’t change the picture itself, you have to delete it, and then replace. So you can amend the necessary section of your code as follows:
Set shp = oSlide.Shapes("Picture") 'Capture properties of the existing picture such as location and size With shp t = .Top l = .Left h = .Height w = .Width End With shp.Delete 'Delete old shape Set shp = oSlide.Shapes.AddPicture("C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg", msoFalse, msoTrue, l, t, w, h) shp.Name = "Picture" shp.ScaleHeight Factor:=1, RelativeToOriginalSize:=msoTrue shp.ScaleWidth Factor:=1, RelativeToOriginalSize:=msoTrue
Of course, you can make your initial shape a rectangle (or other drawing objects) and then Fill it with a picture. In this case, you can always change the Fill and the picture used for it, something like this:
Dim link as String 'set this to the address of the picture you want to use to fill oSlide.Shapes(shp).Fill.UserPicture(link)
But if the original shape is a Picture itself, you usually can’t fill it with another picture.