将 `GroupItems` 的元素转换为 `Shape` [英] Casting elements of `GroupItems` to `Shape`

查看:52
本文介绍了将 `GroupItems` 的元素转换为 `Shape`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a grouped shape in a document:

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

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

AutoShapeType on firstShape returns -2, or msoShapeMixed; and firstShape.GroupItems.Count returns 25:

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

我可以遍历 firstShape.GroupItems 中的形状而无需转换为 Shape.TypeName 为每个元素返回 Shape"AutoShapeType 属性返回 5msoShapeRoundedRectangle:

I can iterate over the shapes in firstShape.GroupItems without casting to Shape. TypeName returns "Shape" for each element, and the AutoShapeType property returns 5, or msoShapeRoundedRectangle:

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

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

But when I try to cast each element to Shape, I get Type mismatch on the first iteration of the For Each:

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

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

How can I extract information about an individual shape from the grouped shape?

推荐答案

使用 Shape 变量在 GroupShapesShape.GroupItems.以下也在迭代中出错:

It seems that it is impossible to enumerate using a Shape variable over the GroupShapes object returned from Shape.GroupItems. The following also errors within the iteration:

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

显然返回的元素不是 Word.Shape 对象,即使 TypeName 函数为此类对象返回 Shape".

Apparently the returned elements are something other than Word.Shape objects, even though the TypeName function returns "Shape" for such objects.

但是,如果我通过 访问元素GroupShapes 集合的 Item 属性,或通过默认属性返回为 Word.Shape 对象,并且可以转换为 Word.Shape:

However, if I access the elements via the 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

这篇关于将 `GroupItems` 的元素转换为 `Shape`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆