我想要执行下面的代码,但使它可以运行在Excel!
ActiveWindow.Selection.SlideRange.SlideIndex
是否有机会在不将宏放入PowerPoint文件的情况下获得选定的幻灯片索引?
发布于 2019-07-19 18:39:32
请尝试使用可能正在运行的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 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
https://stackoverflow.com/questions/57117613
复制相似问题