如何更改 Powerpoint 形状的图像?

How to change image of a Powerpoint shape?

这是我的 PowerPoint 文件: https://www.dropbox.com/s/7my3ubmnv7rxv8y/temp.pptx?dl=0

这是我更改形状图像的代码:

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")

如何更改 Shape 对象的图像?

您尝试做的是创建一个填充,但该方法不起作用,因为所讨论的形状是图片。您可以在 PowerPoint 中自己尝试一下。为图片设置填充没有效果,因为原始图像仍然可见。这就是为什么你看不到任何结果。
你不能改变图片本身,你必须删除它,然后再替换。所以你可以修改你的代码的必要部分如下:

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

当然,您可以将初始形状设为矩形(或其他绘图对象),然后用图片填充。在这种情况下,您可以随时更改 Fill 和用于它的图片,如下所示:

Dim link as String 'set this to the address of the picture you want to use to fill
oSlide.Shapes(shp).Fill.UserPicture(link)

但是如果原来的形状本身就是一个Picture,一般是不能用另外的图片填充的。