【问题标题】:How to check if any shape is selected on a PowerPoint slide?如何检查是否在 PowerPoint 幻灯片上选择了任何形状?
【发布时间】:2021-07-12 13:20:08
【问题描述】:

当前情况:

我有一张幻灯片,上面有几个形状,我希望 vba 代码的行为有所不同,具体取决于用户是否选择了幻灯片上的任何形状(所以根本没有形状)

我试图通过

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

End Sub

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

您可以在哪里提供帮助:

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

【问题讨论】:

    标签: vba powerpoint


    【解决方案1】:

    检查选择类型:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多