Jim Tonic
Jim Tonic

Reputation: 53

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?

Upvotes: 2

Views: 15098

Answers (2)

Asger
Asger

Reputation: 3877

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.

Upvotes: 3

Jim Tonic
Jim Tonic

Reputation: 53

thank you guys a thousend times!!!! Realy appreciate the Help!! Using the variable i bounded to PowerPoint application instead of the one that is linked to the presentation itself did the job for me !!

This is the Code i use now:

Set PowerPoint = CreateObject("Powerpoint.Application")
PowerPoint.Visible = True
PowerPoint.Presentations.Open (pfad & "\Master\Master_Planungsworkshop.pptx")
Set ppApp = GetObject(, "Powerpoint.Application")
Set pppres2 = ppApp.ActivePresentation

input_position = ppApp.ActiveWindow.Selection.SlideRange.SlideIndex

Upvotes: 0

Related Questions