gpt4 book ai didi

excel - 在 Excel VBA 中替换(powerpoint)形状而不更改其位置编号

转载 作者:行者123 更新时间:2023-12-03 02:51:01 24 4
gpt4 key购买 nike

我正在尝试编写应该自动替换 PowerPoint 演示文稿中的某些形状的代码。假定要替换的形状的位置编号已知。我的第一个想法是删除形状,然后向幻灯片添加另一个形状。

sld.Shapes(numShpe).Delete

srcSheet.ChartObjects(chartNum).Copy
sld.Shapes.Paste

问题是,如果我想再次使用此代码,numShpe 将不再是我想要替换的形状的编号。事实上,当使用delete时,shape(n) 变成了 shape(n-1)。所以如果有办法在不改变编号的情况下进行删除,就不会有任何问题了。

您有什么想法可以建议吗?

帖子 2:

我已经尝试了悉达多的建议。这是我的代码的一部分:

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 = slip.Shapes.PasteSpecial(DataType:=10, link:=msoFalse) 上的赋值)

“shapes(未知成员):无效请求。指定的数据类型不可用。”

我不知道它来自哪里......

最佳答案

不要使用数字,而是给形状命名,然后使用它

例如(经过尝试和测试)

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

关于excel - 在 Excel VBA 中替换(powerpoint)形状而不更改其位置编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20472964/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com