这是我的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:\2018\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对象的图像?
答案 0 :(得分:2)
您想要做的是创建一个填充,该填充不起作用,因为所讨论的形状是图片。您可以在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
当然,您可以将初始形状设置为矩形(或其他图形对象),然后填充图片。在这种情况下,您随时可以更改填充以及用于它的图片,如下所示:
Dim link as String 'set this to the address of the picture you want to use to fill
oSlide.Shapes(shp).Fill.UserPicture(link)
但是,如果原始形状是图片本身,则通常无法用其他图片填充它。