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?
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.
CreateObject(progId-that-hits-the-registry-to-locate-the-type) instead of New PowerPoint.Application, since the type is bound at compile-time?CreateObject and New - just edited it.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
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.
Applicationobject? Your question would be easier to answer if you included the code you've got. TrypptApp.ActiveWindow.Selection.SlideRange.SlideIndex, wherepptAppis the PowerPointApplicationinstance.