【问题标题】:Getting the Active Slide of a PPT Presentation via VBA (but from Excel!!)通过 VBA 获取 PPT 演示文稿的活动幻灯片(但来自 Excel !!)
【发布时间】:2019-07-19 18:02:23
【问题描述】:

我想执行下面的代码,但是让它可以在 Excel 中运行!

ActiveWindow.Selection.SlideRange.SlideIndex

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

【问题讨论】:

  • 目标基本上是允许用户在核心演示文稿中选择某个位置,并将幻灯片从云端添加到他选择的某个位置......
  • 您的代码是否有 PowerPoint Application 对象的句柄?如果您包含您拥有的代码,您的问题会更容易回答。试试pptApp.ActiveWindow.Selection.SlideRange.SlideIndex,其中pptApp 是PowerPoint Application 实例。
  • BTW,ActiveWindow.Selection.SlideRange.SlideIndex 如果选择了多张幻灯片,或者没有选择任何幻灯片(当光标位于排序视图中的幻灯片之间或缩略图窗格)。指定范围中的第一张幻灯片将解决第一个问题:ActiveWindow.Selection.SlideRange(1).SlideIndex 并强制视图正常并返回通常会强制选择在第二种情况下的幻灯片上。

标签: excel vba powerpoint


【解决方案1】:

请尝试通过以下方式使用可能正在运行的 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

Here 是对早/晚绑定差异的解释。如果您想按照 cmets 中的建议通过条件编译来实现两者,我找到了一个解释 here

【讨论】:

  • 为什么使用CreateObject(progId-that-hits-the-registry-to-locate-the-type)而不是New PowerPoint.Application,因为类型是在编译时绑定的?
  • 如果您打算使用后期绑定的代码,那么如果您使用条件编译仅通过设置一个标志来支持后期绑定和早期绑定,那将是一个更好的例子。
  • @Mathieu 感谢您指出CreateObjectNew 的区别 - 刚刚编辑了它。
  • ppApp.ActiveWindow.Selection.SlideRange(1) 如果未选择任何内容,则会返回错误
  • @ThierryDalon 是的,谢谢。如果选择了幻灯片,我只是添加了一个检查。
【解决方案2】:

谢谢你们千倍!!!!非常感谢帮助! 使用我绑定到 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

【讨论】:

  • 该代码对于它所使用的 PPT 实例的模棱两可非常危险。为什么你需要ppApp,而PowerPoint 已经是一个活动的PowerPoint.Application 实例?而且如果引用了PPT类型库,就不用CreateObject了,没用的低效……就New吧。当代码停止运行时,请确保打开任务管理器并验证所有 powerpoint.exe 进程是否已完全退出。我怀疑如果在 PowerPoint 已经打开时运行此代码,PowerPoint 实例可能永远不会退出。不管怎样,很高兴你能成功。
  • 感谢您的输入,对代码进行了相当多的改进!
  • @MathieuGuindon 至于最好的方法,你的建议是有针对性的。但是 FWIW,PPT 通常会启动自身的多个实例,这与 Excel/Word 不同。在某些版本的 PPT 中检查 TaskMan 可能会有点误导。如果您将 Win Explorer 设置为查看预览并选择一个 PPT 文件,预览过程将启动一个隐藏的 powerpnt.exe 实例(它是 powerpnt,而不是 powerpoint,顺便说一句)。这有时会导致奇怪的其他问题,因为 PPT 知道它自己只能有一个实例,并且当有两个实例时会感到不安。
猜你喜欢
  • 1970-01-01
  • 2013-06-07
  • 1970-01-01
  • 2023-02-10
  • 1970-01-01
  • 2021-04-18
  • 2014-02-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多