How to replace image of a picture shape in PowerPo

2019-09-06 02:50发布

问题:

Is there any way to replace the image of an existing picture shape in PowerPoint from code?

Thanks

回答1:

If it's a linked image, either overwrite the linked image file with a new file or point the link at a new file.

Or if the shape is picture filled, you can fill it with a different picture instead. That can distort the picture if its proportions don't match the shape's proportions; instead, you can change the aspect ratio of the shape to match that of the picture if need be.

To change the picture fill, assuming a reference to the shape in osh:

With osh.Fill
    .UserPicture ("C:\path\to\picture.jpg")
End With

To work out the size of the picture so you can adjust the shape to it, you can either use external image processing software or insert the picture into PPT, giving -1 for height and width parameters and pick up the .Height and .Width of the newly inserted picture, then delete it.

Or insert a new picture, pick up the old picture's size/position/other proprties and apply them to the new picture, delete the old picture, move the new one to the old one's Z-order.