首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >通过VBA获取PPT演示文稿的活动幻灯片(但来自Excel!)

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

Stack Overflow用户
提问于 2019-07-19 18:02:23
回答 1查看 9.8K关注 0票数 2

我想要执行下面的代码,但使它可以运行在Excel!

代码语言:javascript
运行
复制
ActiveWindow.Selection.SlideRange.SlideIndex

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-19 18:39:32

请尝试使用可能正在运行的PowerPoint实例如下:

代码语言:javascript
运行
复制
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 x.x对象库“添加了一个VBA引用,用于早期绑定和智能感知。

以下是后期绑定的备选方案:

代码语言:javascript
运行
复制
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是早期/晚期结合差异的一种解释。如果您想按照注释中的建议通过条件编译来实现这两者,我找到了一个解释here

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57117613

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档