将' GroupItems '中的元素转换为' Shape '



我在文档中创建了一个分组形状:

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

AutoShapeTypeonfirstShape返回-2,或msoShapeMixed;和firstShape.GroupItems.Count返回25:

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

我可以在firstShape.GroupItems的形状上迭代,而不投射到ShapeTypeName为每个元素返回"Shape",AutoShapeType属性返回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

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

似乎不可能使用形状进行枚举变量GroupShapesShape返回的对象。GroupItems. 在迭代中也会出现以下错误:

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

显然返回的元素不是Word。对象,即使TypeName函数返回这些对象的"Shape"

但是,如果我通过Item访问元素属性GroupShapes集合,或者通过默认属性,它们将作为Word返回。对象,并且可以强制转换为Word。:
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

最新更新