如何查看Shape.ParentGroup?

How to check Shape.ParentGroup?

如何在不使用 On Error 语句的情况下检查 Shape.ParentGroup 是否存在?

以下代码不起作用:

Function IsMemberOfGroup(Shape_1 As Shape) as Boolean

    If Shape_1.ParentGroup Is Nothing Then
        Debug.Print "This shape is not a member of a group"
        IsMemberOfGroup = False
    Else
        Debug.Print "This shape is a member of a group"
        IsMemberOfGroup = True
    End If

End Function

ExcelVBA中的形状对象属性:

P.S.
Shape.ParentGroupNull 进行比较也不起作用。

您可以检查形状是否为 Child,然后它有 ParentGroup

Function IsMemberOfGroup(Shape_1 As Shape) As Boolean

    If Not Shape_1.Child Then
        Debug.Print "This shape is not a member of a group"
        IsMemberOfGroup = False
    Else
        Debug.Print "This shape is a member of a group"
        IsMemberOfGroup = True
    End If

End Function

Ricardo 的回答是一个优雅的解决方案。我对你的确切问题有点偏离目标但是,要在 sheet 上列出所有形状并说明它们是独立形状,还是组形状或组成员 - 这应该有效 - 只要Grouped Shape 名称以“Group”开头 - 这是默认值

Sub ChkALLShapes()
   Dim xShp As Shape
   
   For Each xShp In ActiveSheet.Shapes
      If Left(xShp.Name, 5) = "Group" Then
         Debug.Print xShp.Name & " is a GroupShape"
         For K = 1 To xShp.GroupItems.Count
            Debug.Print xShp.GroupItems.Item(K).Name & " is a member of the Group"
         Next K
      Else
         Debug.Print xShp.Name & " is a StandAlone Shape"
      End If
   Next xShp
End Sub

编辑:修正了一些错字

将 Ricardo 的答案与 .Type 一起使用 属性

Function IsGrpMember(dShp As Shape) As Boolean
    If dShp.Child = True Then
       If (dShp.ParentGroup.Type = msoGroup) Then
          IsGrpMember = True
       End If
    End If
End Function