下面的代码使用Late Binding to PowerPoint(因此您不需要添加对PowerPoint库的引用),并检查PowerPoint实例是否已打开。
之后,它将ActivePresentation
设置为ppPres
。
最后,您向后循环删除从结尾到第二张幻灯片的所有幻灯片(只保留第一张幻灯片)。
注意:您可以很容易地修改For i = ppPres.Slides.Count To 2 Step -1
循环,以满足您的需求。
代码
Option Explicit
Sub DEL()
Dim ppProgram As Object
Dim ppPres As Object
Dim ppSlide As Object
Dim i As Long
On Error Resume Next
Set ppProgram = GetObject(, "PowerPoint.Application")
On Error GoTo 0
' check if PowerPoint instance is open >> if not raise an error
If ppProgram Is Nothing Then
MsgBox "PowerPoint is closed!"
Exit Sub
Else
' set the ppPres object to active PowerPoint presentation
Set ppPres = ppProgram.ActivePresentation
' always loop backwards when deleting objects (in this case slides)
For i = ppPres.Slides.Count To 2 Step -1
Set ppSlide = ppPres.Slides(i)
ppSlide.Delete
Next i
End If
End Sub
什么错误? –
请在发布问题前先搜索stackoverflow。 –