将 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)
AutoShapeType
在 firstShape
returns -2
或 msoShapeMixed
上;和 firstShape.GroupItems.Count
returns 25
:
Debug.Print firstShape.AutoShapeType
Debug.Print firstShape.GroupItems.Count
我可以遍历 firstShape.GroupItems
中的形状而无需强制转换为 Shape
。 TypeName
returns "Shape"
对于每个元素,以及 AutoShapeType
属性 returns 5
或 msoShapeRoundedRectangle
:
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
我在文档中有一个分组形状:
Dim doc As Word.Document
Set doc = Documents("dlr-overview.docx")
Dim firstShape As Word.Shape
Set firstShape = doc.Shapes(1)
AutoShapeType
在 firstShape
returns -2
或 msoShapeMixed
上;和 firstShape.GroupItems.Count
returns 25
:
Debug.Print firstShape.AutoShapeType
Debug.Print firstShape.GroupItems.Count
我可以遍历 firstShape.GroupItems
中的形状而无需强制转换为 Shape
。 TypeName
returns "Shape"
对于每个元素,以及 AutoShapeType
属性 returns 5
或 msoShapeRoundedRectangle
:
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