gpt4 book ai didi

excel - 通过 VBA 获取 PPT 演示文稿的事件幻灯片(但来自 Excel !!)

转载 作者:行者123 更新时间:2023-12-04 20:09:35 27 4
gpt4 key购买 nike

我想执行以下代码,但让它可以在 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
我为早期绑定(bind)和智能感知添加了对“Microsoft PowerPoint x.x 对象库”的 VBA 引用。
这是后期绑定(bind)的替代方案:
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是对早期/晚期绑定(bind)差异的解释。如果您想按照评论中的建议通过条件编译来实现两者,我找到了一个解释 here .

关于excel - 通过 VBA 获取 PPT 演示文稿的事件幻灯片(但来自 Excel !!),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57117613/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com