2

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?

Asger
  • 3,822
  • 3
  • 12
  • 37
Jim Tonic
  • 53
  • 1
  • 2
  • 4
  • The Goal is basicly to allow the user to slected a certain Position in an core presentation and add slides out of a cloud into the certain Position, which he selected.... – Jim Tonic Jul 19 '19 at 18:23
  • Does your code have a handle on the PowerPoint `Application` object? Your question would be easier to answer if you included the code you've got. Try `pptApp.ActiveWindow.Selection.SlideRange.SlideIndex`, where `pptApp` is the PowerPoint `Application` instance. – Mathieu Guindon Jul 19 '19 at 18:37
  • BTW, ActiveWindow.Selection.SlideRange.SlideIndex will throw an error if more than one slide is selected, or if no slide is selected (as can happen when the cursor is between slides in sorter view or in the thumbnail pane). Specifying the first slide in the range will solve the first problem: ActiveWindow.Selection.SlideRange(1).SlideIndex and forcing the view to Normal and back will generally force the selection onto the slide just prior in the second case. – Steve Rindsberg Jan 20 '21 at 20:16

2 Answers2

2

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.

Asger
  • 3,822
  • 3
  • 12
  • 37
  • Why use `CreateObject(progId-that-hits-the-registry-to-locate-the-type)` instead of `New PowerPoint.Application`, since the type is bound at compile-time? – Mathieu Guindon Jul 19 '19 at 18:46
  • If you meant to use the code late-bound, it would make for better example if you used conditional compilation to support both late and early binding just by setting a flag. – this Jul 19 '19 at 18:53
  • @Mathieu Thanks for pointing me to this difference of `CreateObject` and `New` - just edited it. – Asger Jul 19 '19 at 20:29
  • ppApp.ActiveWindow.Selection.SlideRange(1) returns an error if nothing is selected – Thierry Dalon Jan 13 '21 at 09:00
  • @ThierryDalonYes, thanks. I just added a check, if a slide is selected. – Asger Jan 20 '21 at 13:05
0

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
Jim Tonic
  • 53
  • 1
  • 2
  • 4
  • That code is dangerously ambiguous about what instance of PPT it's working with. Why to you need `ppApp`, when `PowerPoint` is already an active `PowerPoint.Application` instance? And if the PPT type library is referenced, you don't need to `CreateObject`, it's uselessly inefficient... just `New` it up. When the code stops running, make sure you bring up Task Manager and verify that all powerpoint.exe processes have cleanly exited. I suspect the `PowerPoint` instance might never exit if this code runs while PowerPoint is already open. Anyway, glad you got it working. – Mathieu Guindon Jul 19 '19 at 19:55
  • Thank you for your Input, there are quite some improvements to make to the code ! – Jim Tonic Aug 01 '19 at 17:56
  • 1
    @MathieuGuindon As far as the best way of doing this, your suggestions are on target. But FWIW, PPT normally fire up multiple instances of itself, unlike Excel/Word. Checking TaskMan can be a bit misleading with some versions of PPT. If you have Win Explorer set to view previews and select a PPT file, the preview process will fire up a hidden instance of powerpnt.exe (it's powerpnt, not powerpoint, btw). Which can sometimes cause strange other problems because PPT knows it can only have one instance of itself and gets an upset tummy when there are two. – Steve Rindsberg Jan 21 '21 at 16:55