VBA删除powerpoint中的活动幻灯片

时间:2017-06-26 11:19:38

标签: excel vba excel-vba

我正在尝试删除Active Powerpoint Presentation中的幻灯片,但我在 sld_id = ActiveWindow.Selection.SlideRange.SlideIndex

上有错误
Sub DEL()
  Dim i As Long
  Dim sld_id As Long
  sld_id = ActiveWindow.Selection.SlideRange.SlideIndex
  With ActivePresentation.Slides
    For i = .Count To sld_id Step -1
      .Item(i).Delete
    Next i
  End With
End Sub

任何人都可以帮助我吗? 非常感谢! Roxana的

1 个答案:

答案 0 :(得分:2)

下面的代码使用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
相关问题