Search code examples
vbavstopowerpointpowerpoint-2007

How do I tell where the insertion point is in the Slides pane?


In PowerPoint, in "normal" view, the window is split into two panes, with a pane showing slide thumbnails on the left, and a pane showing the current slide on the right. You can select more than one slide in the left-hand panel, which is useful if you want to copy, move or delete slides.

To tell which slide(s) are currently selected in the left-hand panel, you can use ActiveWindow.Selection.SlideRange. However, if you click between slides in the left-hand (thumbnail) panel, you end up with an insertion point, and:

  • ActiveWindow.Selection.Type is zero (ppSelectionNone).
  • ActiveWindow.Selection.SlideRange gives an error.

I have a question in two halves:

  1. How can I detect this situation? (Presumably there are other cases where the selection type is "none").
  2. How can I tell where the insertion point is, so that I can insert new slides at that point?

Either VBA or VSTO code would be fine :-)


Solution

  • Answer to the first question:

    ' The mouse cursor can be placed between slide thumbnails in the following views:
    ' - Normal View / Thumbnail Pane
    ' - Slide Sorter View
    ' - Slide Master View / Thumbnail Pane
    ' Returns true if the cursor is in the slide gap in these cases.
    Function IsSlideGap() As Boolean
        On Error Resume Next
    
        With ActiveWindow
    
            Select Case .View.Type
                Case ppViewNormal, ppViewSlideMaster
                    ' If the thumbnail pane (ViewType 11 or 12 ) is active but                                       
                    ' nothing is selected, we must be in the slide gap
                    If .Panes(1).Active And .Selection.Type = ppSelectionNone Then IsSlideGap = True
                Case ppViewSlideSorter
                    If .Selection.Type = ppSelectionNone Then IsSlideGap = True
            End Select
    
        End With
    End Function
    

    Answer to second question:

    ' Note: used by slide/gap context menus only so assumes
    ' either thumbnail pane or sorter view active
    Function GetSlideCursorIndex() As Long
        Dim lSlides As Long ' Track the number of slides in order
                            ' to check if the temporary slide was deleted.
        Dim oSld As Slide
    
        lSlides = ActivePresentation.Slides.Count
    
        ' Insert a temporary new slide
        CommandBars.ExecuteMso "SlideNew"
    
        DoEvents
    
        Set oSld = ActiveWindow.View.Slide
    
        With oSld
            GetSlideCursorIndex = .SlideIndex
            .Delete
        End With
    
        If ActivePresentation.Slides.Count <> lSlides Then Debug.Print "Something went wrong!"
    End Function