当SelectionType = ppSelectionNone时,如何获得Powerpoint中可见幻灯片的SlideIndex

Dav*_*ens 6 powerpoint powerpoint-vba

我有一些代码需要我知道SlideIndex要操作的内容(例如,插入新幻灯片的位置,插入ChartObject的位置等).大约99%的时间,我可以成功获得SlideIndex:

Dim w as Long 'slide index variable
w = ActivePresentation.Windows(1).Selection.SlideRange(1).SlideIndex
Run Code Online (Sandbox Code Playgroud)

另外0.1%的时间,ActivePresentation.Windows(1).SelectionType = ppSelectionNone它会失败,因为(可以理解)它无法获得SlideIndex选择,因为没有选择.如果用户无意中"选择"了"大纲"窗格中两个幻灯片之间的空间,则可能会发生这种情况.

理想情况下,我想要做的是获取SlideIndex幻灯片的属性,该属性在幻灯片窗格中可见:

在此输入图像描述 我目前有一些代码测试是否SelectionTypeppSelectionNone,所以我可以捕获条件,我只是没有找到一种方法来识别幻灯片窗格的slideIndex.

Function GetMySlide()
Dim w as Long
    If Not ActivePresentation.Windows(1).Selection.Type = ppSelectionNone Then

        w = ActivePresentation.Windows(1).Selection.SlideRange(1).SlideIndex
        Set GetMySlide = ActivePresentation.Slides(w)

    Else:

        MsgBox "No slide is currently selected. Please select a slide in the Outline pane in order to proceed.", vbInformation
        Set GetMySlide = Nothing
        Exit Function
    End If
End Function
Run Code Online (Sandbox Code Playgroud)

更新

我的临时解决方案是使用公共变量lastUsedSlide来试图跟踪最近选择的幻灯片.我可以将此与WindowSelectionChange事件结合起来,但希望有一个更直接的解决方案.如果我认为这种方法总是有效,我会使用它,但是,它可能引入无法预料的错误,因为lastUsedSlide它不是一个可靠的代理what_slide_i_am_currently_looking_at.

Kaz*_*wor 3

大卫,也许你可以对这样的对象使用额外的Activate方法:Window.Pane

'new code:
ActivePresentation.Windows(1).Panes(2).Activate
'your code
Dim w as Long 'slide index variable
w = ActivePresentation.Windows(1).Selection.SlideRange(1).SlideIndex
Run Code Online (Sandbox Code Playgroud)

但是,请阅读更多有关Pane.ViewType财产的信息,这可能会有所帮助。在我的简单测试中, 和Panes(2)Panes(3)有效,但是您可能有不同的调用子程序的上下文。