'PowerPoint - Determine whether the Cursor is before the first slide or after it

I need to determine the cursor position in the slide-pane in PowerPoint, in order to insert a new slide in the correct position.

When a slide is selected, the current slide number is Application.ActiveWindow.View.Slide.SlideIndex. When the cursor is between slides, switching to another view and then back (e.g., to Slide View and then back to Slide Sorter) selects the slide immediately before the cursor, and then the method above works.

The problem is in one case: when the cursor is positioned before the first slide or after it. Switching the view back and forth, positions the cursor on the first slide in both cases.

How can I determine if the cursor position in the Slide-Pane is before the first slide or after it?



Solution 1:[1]

Good question.

One ugly method would be to use SendKeys to squirt a downarrow key. If the cursor's above the first slide, that will cause the first slide to be selected. If the cursor's between slides one and two, then the second slide will be selected.

Slightly less ugly would be to use the .Execute method to fire off the correct control for inserting a new slide, which should mimic what the user gets when they do the same thing; if the inserted slide's index is 1, then the cursor was above the original first slide. If the inserted slide's index is 2, the cursor was originally between slides 1 and 2.

Or use SendKeys ("^M") to accomplish the same thing (won't work from w/in the IDE but in PPT proper you can Alt+F8 and choose the macro to run it.)

Ex: this displays 1 if the cursor was above the first slide, 2 if it was between slides 1 and 2. The DoEvnts is necessary; otherwise PPT hasn't created the slide before the next line runs and it errors out.

Sub thing()
    Dim lTemp As Long
    SendKeys ("^M")
    DoEvents
    lTemp = ActiveWindow.Selection.SlideRange(1).SlideIndex
    ActiveWindow.Selection.SlideRange.Delete
    MsgBox lTemp
End Sub

I suspect you'd want to add some safety checks to make sure that an existing slide doesn't get mistakenly deleted rather than the one you've just inserted.

Solution 2:[2]

Agreed, SendKeys is bad, .Execute safer but still a bit risky.

You can iterate the panes collection and check the ViewType of the active pane w/o changing the active pane or the user's selection point. If the ViewType = 11, the selection point's in the thumbnails pane (and then you'd want to do the Insert Dummy Slide trick to figure out exactly where in the thumbnails pane it is).

Sub OhThePane()
    Dim x As Long
    With ActiveWindow
        For x = 1 To .Panes.Count
            If .Panes(x).Active Then
                MsgBox "Pane: " & CStr(x) & vbCrLf & .Panes(x).ViewType
            End If
        Next
    End With
End Sub

Note: if you run this from within PPT, you have to use Alt+F8 to launch it. Using the menu bar/ribbon tab will move the focus away from the user-selected pane and give you bogus results.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Steve Rindsberg
Solution 2 Steve Rindsberg