如果你只有一組在Word的形狀,這將工作,當你想將其分配到的形狀:
Sub FindOle()
Dim shp As Shape
Dim allShapes As Shape
Dim c As Long
For Each shp In ActiveDocument.Shapes
Debug.Print shp.Name
Set allShapes = shp
Next shp
Debug.Print allShapes.Name
End Sub
一些變通辦法後,這裏是用GroupShapes
類的好方法:
Option Explicit
Sub FindOle()
Dim shp As Shape
Dim allShapes As GroupShapes
Dim cnt As Long
With ActiveDocument.Shapes
.AddShape(msoShapeIsoscelesTriangle, 10, 10, 100, 100).Name = "shp1"
.AddShape(msoShapeIsoscelesTriangle, 150, 10, 100, 100).Name = "shp2"
.AddShape(msoShapeIsoscelesTriangle, 300, 10, 100, 100).Name = "shp3"
'assign the shapes to a group
With .Range(Array("shp1", "shp2", "shp3")).Group
Set allShapes = .GroupItems
End With
'format the first and the third shape, prints the name of the shape:
For cnt = 1 To allShapes.Count
Debug.Print allShapes.Item(cnt).Name
If cnt/2 <> 1 Then
allShapes.Item(cnt).Fill.PresetTextured msoTextureGreenMarble
End If
Next cnt
'print the name of the shapes in a different way:
For cnt = 1 To allShapes.Count
Debug.Print .Range(Array("shp1", "shp2", "shp3"))(cnt).Name
Next cnt
End With
End Sub
在一個空的Word文檔它創建3周的形狀,並將其分配給一個組,並通過allShapes
變量或通過.Range(Array())
訪問它們。
GroupShapes MSDN
感謝@Vityata,我可以輕鬆地訪問ActiveDocument.Shapes對象和裏面GroupItems。主要問題是ActiveDocument.InlineShapes.GroupItem,它引發訪問衝突 – fluffybunny