通过 VBA 获取 PPT 演示文稿的活动幻灯片(但来自 Excel!!)

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

我想执行以下代码,但要使其在 Excel!

后可运行
ActiveWindow.Selection.SlideRange.SlideIndex

是否有机会在不将宏放入 PowerPoint 文件的情况下获取选定的幻灯片索引?

请尝试使用可能 运行 的 PowerPoint 实例:

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

我添加了对“Microsoft PowerPoint x.x 对象库”的 VBA 引用,用于早期绑定和智能感知。

这是后期绑定替代方案:

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

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 .

谢谢你们千百遍!!!!非常感谢帮助! 使用绑定到 PowerPoint 应用程序的变量而不是链接到演示文稿本身的变量为我完成了工作!

这是我现在使用的代码:

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