Geole
Geole

Reputation: 376

How to check if any shape is selected on a PowerPoint slide?

CURRENT SITUATION:

I have a slide with several shapes on it and I want the vba code to behave differently depending on whether the user has selected any shape at all on the slide or not (so no shape at all)

I tried to get the number of selected shapes via

Sub DETERMINE_IF_ANY_SHAPE_IS_SELECTED ()
    
    Debug.print ActiveWindow.Selection.ShapeRange.Count

End Sub

but this throws an error because "nothing" was selected to be counted. I don't get back the value 0 as I had hoped for.

WHERE YOU MIGHT HELP:

Ist there a way to determine if any (!) shape was selected at all? Or can I just address this via error handling?

Upvotes: 0

Views: 1867

Answers (1)

braX
braX

Reputation: 11735

Check the selection type:

Sub test()
   If ActiveWindow.Selection.Type = ppSelectionShapes Then
     Debug.Print ActiveWindow.Selection.ShapeRange.Count & " shapes selected"
   Else
     Debug.Print "no shapes are selected"
   End If
End Sub

If it is ppSelectionNone (0) then nothing is selected.

So you can also check:

If ActiveWindow.Selection.Type = ppSelectionNone Then

Error handling is very rarely required.

https://learn.microsoft.com/en-us/office/vba/api/powerpoint.selection

To handle them all:

Select Case ActiveWindow.Selection.Type
  Case ppSelectionNone
    ' what to do when nothing is selected

  Case ppSelectionShapes
    ' what to do when shapes are selected

  Case ppSelectionSlides
    ' what to do when slides are selected

  Case ppSelectionText
    ' what to do when text is selected

End Select

Upvotes: 2

Related Questions