Search code examples
excelvbapowerpoint

Getting the Active Slide of a PPT Presentation via VBA (but from Excel!!)


I would like to perform following code, but make it runnable out of Excel!

ActiveWindow.Selection.SlideRange.SlideIndex

Is there any chance to get the selected slide index without putting a macro into the PowerPoint file?


Solution

  • Please try to use a possibly running instance of PowerPoint by this:

    Private Sub ControlPowerPointFromExcelEarlyBinding()
        Dim ppApp As PowerPoint.Application
        Dim ppPres As PowerPoint.Presentation
        Dim ppSlide As PowerPoint.Slide
        
        ' try to address PowerPoint if it's already running
        On Error Resume Next
        Set ppApp = GetObject(, "PowerPoint.Application")
        On Error GoTo 0
        
        If Not ppApp Is Nothing Then                ' PowerPoint is already running
            Set ppPres = ppApp.ActivePresentation   ' use current presentation
            If ppPres Is Nothing Then               ' if no presentation there
                Set ppPres = ppApp.Presentations.Open("...")    ' open it
            End If
        Else                                        ' new PowerPoint instance necessary
            Set ppApp = New PowerPoint.Application  ' start new instance
            Set ppPres = ppApp.Presentations.Open("...")    ' open presentation
        End If
        
        ppApp.Visible = True
        ppApp.Activate
        
        If ppApp.ActiveWindow.Selection.Type = ppSelectionSlides Then
            Set ppSlide = ppApp.ActiveWindow.Selection.SlideRange(1)
            ' or Set ppSlide = ppApp.ActiveWindow.View.Slide
        End If
        Debug.Print ppSlide.SlideID, ppSlide.SlideNumber, ppSlide.SlideIndex
    End Sub
    

    I added a VBA reference to "Microsoft PowerPoint x.x Object Library" for early binding and intellisense.

    Here's the late binding alternative:

    Private Sub ControlPowerPointFromExcelLateBinding()
        Dim ppApp As Object
        Dim ppPres As Object
        Dim ppSlide As Object
                
        On Error Resume Next
        Set ppApp = GetObject(, "PowerPoint.Application")
        On Error GoTo 0
        
        If Not ppApp Is Nothing Then
            Set ppPres = ppApp.ActivePresentation
            If ppPres Is Nothing Then
                Set ppPres = ppApp.Presentations.Open("...")
            End If
        Else
            Set ppApp = CreateObject("PowerPoint.Application")
            Set ppPres = ppApp.Presentations.Open("...")
        End If
        
        ppApp.Visible = True
        ppApp.Activate
        
        If ppApp.ActiveWindow.Selection.Type = ppSelectionSlides Then
            Set ppSlide = ppApp.ActiveWindow.Selection.SlideRange(1)
            ' or Set ppSlide = ppApp.ActiveWindow.View.Slide
        End If
        Debug.Print ppSlide.SlideID, ppSlide.SlideNumber, ppSlide.SlideIndex
    End Sub
    

    Here is an explanation of the early/late binding difference. If you want to implement both via conditional compilation as suggested in the comments, I found an explanation here.