将 GroupItems 的元素转换为 Shape

Casting elements of `GroupItems` to `Shape`

我在文档中有一个分组形状:

Dim doc As Word.Document
Set doc = Documents("dlr-overview.docx")
Dim firstShape As Word.Shape
Set firstShape = doc.Shapes(1)

AutoShapeTypefirstShape returns -2msoShapeMixed 上;和 firstShape.GroupItems.Count returns 25:

Debug.Print firstShape.AutoShapeType
Debug.Print firstShape.GroupItems.Count

我可以遍历 firstShape.GroupItems 中的形状而无需强制转换为 ShapeTypeName returns "Shape" 对于每个元素,以及 AutoShapeType 属性 returns 5msoShapeRoundedRectangle:

Dim x As Variant
For Each x In firstShape.GroupItems
    Debug.Print TypeName(x)
    Debug.Print x.AutoShapeType
Next

但是当我尝试将每个元素转换为 Shape 时,我在 For Each 的第一次迭代中得到 类型不匹配:

Dim subshape As Shape
For Each subshape In firstShape.GroupItems
    Debug.Print subshape.Left
Next

如何从分组形状中提取有关单个形状的信息?

似乎无法使用Shape variable over the GroupShapes object returned from Shape.GroupItems进行枚举。迭代中还有以下错误:

Dim shapes As GroupShapes
Set shapes = firstShape.GroupItems
Dim shp As Shape
For Each shp In shapes
    Debug.Print shp.Name
Next

显然,对于此类对象,返回的元素不是 Word.Shape objects, even though the TypeName 函数 returns "Shape"

但是,如果我通过 Item property of the GroupShapes collection, or via the default property, they are returned as Word.Shape objects, and can be casted to Word.Shape:

访问元素
Dim i As Integer
For i = 1 To firstShape.GroupItems.Count
    Dim shp As Shape
    Set shp = firstShape.GroupItems(i)
    Debug.Print shp.Name
Next