2017-06-26 49 views
-1

我试图删除在Active PPT演示幻灯片,但我有一个错误sld_id = ActiveWindow.Selection.SlideRange.SlideIndexVBA来删除活动的幻灯片在PowerPoint

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

+0

什么错误? –

+0

请在发布问题前先搜索stackoverflow。 –

回答

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 
+1

++不错的一个...'ppPres.Slides(i).Delete'会将其缩短得更远:D –

+1

@SiddharthRout真的,我想这是自动的,我定义并设置每个对象。 (顺便说一句,爱你的自制月历日历) –

+0

非常感谢你的帮助!其他帖子中的VBA代码没有为我工作。对于这些,我在Set Pre = ActivePresentation方面也出现了错误。 –