如何检查是否在 PowerPoint 幻灯片上选择了任何形状?

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

当前情况:

我有一张幻灯片,上面有几个形状,我希望 vba 代码根据用户是否在幻灯片上选择 任何形状而表现不同(所以完全没有形状)

我试图通过

获取所选形状的数量
Sub DETERMINE_IF_ANY_SHAPE_IS_SELECTED ()
    
    Debug.print ActiveWindow.Selection.ShapeRange.Count

End Sub

但这会引发错误,因为选择了“无”进行计数。我没有像我希望的那样取回值 0。

您可能需要帮助的地方:

有没有办法确定是否选择了任何 (!) 形状?或者我可以通过错误处理来解决这个问题吗?

检查选择类型:

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

如果是 ppSelectionNone (0) 则什么都没有选择。

所以你也可以检查:

If ActiveWindow.Selection.Type = ppSelectionNone Then

很少需要错误处理。

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

全部处理:

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