当 SelectionType = ppSelectionNone 时,如何获取 Powerpoint 中可见幻灯片的 SlideIndex [英] How can I get the SlideIndex of a visible Slide in Powerpoint when SelectionType = ppSelectionNone
问题描述
我有一些代码要求我知道要操作的 SlideIndex
(例如,在何处插入新幻灯片、在何处插入 ChartObject 等).大约 99% 的情况下,我可以通过以下方式成功获取 SlideIndex
:
I have some code that requires me to know what SlideIndex
to operate on (e.g., where to insert a new slide, where to insert a ChartObject, etc.). About 99% of the time, I can successfully obtain the SlideIndex
by:
Dim w as Long 'slide index variable
w = ActivePresentation.Windows(1).Selection.SlideRange(1).SlideIndex
另外 0.1% 的时间,当 ActivePresentation.Windows(1).SelectionType = ppSelectionNone
时,它会失败,因为(可以理解)它无法获得 SlideIndex选择的代码>,因为没有选择.如果用户在大纲"窗格中无意中选择"了两张幻灯片之间的空间,则可能会发生这种情况.
The other 0.1% of the time, when ActivePresentation.Windows(1).SelectionType = ppSelectionNone
, it will fail, because (understandably) it can't obtain the SlideIndex
of the selection, because there is no selection. This might occur if the user has inadvertently "selected" the space between two slides in the Outline pane.
理想情况下,我想要做的是获取在幻灯片窗格中可见的幻灯片的 SlideIndex
属性:
What I would like to do, ideally, is get the SlideIndex
property of the slide which is visible in the Slides pane:
我目前有一些代码可以测试 SelectionType
是否为 ppSelectionNone
,所以我可以捕获条件,我只是没有想出一种方法来识别幻灯片窗格的 slideIndex.
I currently have some code that tests whether the SelectionType
is ppSelectionNone
, so I can trap the condition, I just have not figured a way to identify the slideIndex of the Slides Pane.
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
更新
我的临时解决方案是使用公共变量 lastUsedSlide
来尝试跟踪最近选择的幻灯片.我可以将其与 WindowSelectionChange
事件合并,但希望有一个更直接的解决方案.如果我认为这种方法总是有效,我会使用它,但是,它可能会引入不可预见的错误,因为 lastUsedSlide
不是 what_slide_i_am_currently_looking_at<的可靠代理代码>.
My interim solution is to use a public variable lastUsedSlide
in an attempt to keep track of the most recently selected Slide. I can incorporate this with the WindowSelectionChange
event, but was hoping there would be a more straightforward solution. If I thought this method would always work, I would use it, however, it potentially introduces unforeseen errors, since the lastUsedSlide
is not a reliable proxy for what_slide_i_am_currently_looking_at
.
推荐答案
David,也许你可以像这样为 Window.Pane
对象额外使用 Activate
方法:>
David, maybe you could use additionally Activate
method for Window.Pane
object like this:
'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
但是,请阅读有关 Pane.ViewType
属性的更多信息,这可能会有所帮助.在我的简单测试中,Panes(2)
和 Panes(3)
都可以工作,但您可能有不同的调用子程序的上下文.
However, please read some more about Pane.ViewType
property which could be helpful. In my simple test both Panes(2)
and Panes(3)
works but you could have different context of calling your sub.
这篇关于当 SelectionType = ppSelectionNone 时,如何获取 Powerpoint 中可见幻灯片的 SlideIndex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!