我正在尝试编写应自动替换PowerPoint演示文稿中某些形状的代码。假设已知要替换的形状的位置编号。 我的第一个想法是删除形状,然后在幻灯片中添加另一个形状。
sld.Shapes(numShpe).Delete
srcSheet.ChartObjects(chartNum).Copy
sld.Shapes.Paste
问题是,如果我想再次使用此代码,numShpe
将不再是我想要替换的形状的数量。实际上,当使用delete
时,shape(n)变为形状(n-1)。
因此,如果有一种删除方式而不更改编号,则不会有任何问题。
你有什么想法可以建议吗?
帖子2:
我尝试了Siddharth的建议。这是我的代码的一部分:
Set sld = pres.Slides(slideNum)
Dim shpeName As String
shpeName = "Picture 15"
Dim shpe As PowerPoint.Shape
Set shpe = sld.Shapes(shpeName)
shpe.Delete
srcSheet.ChartObjects(chartNum).Copy
Set shpe = sld.Shapes.PasteSpecial(DataType:=10, link:=msoFalse)
shpe.Name = shpeName
运行时,我收到以下错误(关于Set shpe = slide.Shapes.PasteSpecial(DataType:=10, link:=msoFalse)
行的分配)
“shapes(未知成员):无效请求。指定的数据类型不可用。”
我无法找到它的来源......
答案 0 :(得分:3)
不是使用数字,而是给形状命名,然后使用
例如( Tried And Tested )
Sub Sample()
Dim oPPApp As Object, oPPPrsn As Object
Dim oPPSlide As Object, oPPShape As Object
Dim FlName As String
Dim chartNum As Long
'~~> Change this to the relevant file
FlName = "C:\MyFile.PPTX"
'~~> Establish an PowerPoint application object
On Error Resume Next
Set oPPApp = GetObject(, "PowerPoint.Application")
If Err.Number <> 0 Then
Set oPPApp = CreateObject("PowerPoint.Application")
End If
Err.Clear
On Error GoTo 0
oPPApp.Visible = True
'~~> Open the relevant powerpoint file
Set oPPPrsn = oPPApp.Presentations.Open(FlName)
'~~> Change this to the relevant slide which has the shape
Set oPPSlide = oPPPrsn.Slides(1)
'~~> This is the shape which will be replaced
Set oPPShape = oPPSlide.Shapes("MyShape")
oPPShape.Delete
chartNum = 1
ThisWorkbook.Sheets("Sheet1").ChartObjects(chartNum).Copy
Set oPPShape = oPPSlide.Shapes.PasteSpecial(DataType:=10, Link:=msoFalse)
oPPShape.Name = "MyShape"
End Sub