2017-12-18 153 views
2

我想在Word文档中找到ole对象,而且它似乎在InlineShapes(1).GroupItems。但不能访问组项目,因为它给我错误。此成员只能访问一个组VBA Word文档

Sub findOle() 
    Dim shp As GroupShapes 
    Dim c As Integer 
    Set shp = ActiveDocument.InlineShapes(1).GroupItems 
End Sub 

这个成员只能为一组

我能够访问ActiveDocument.Shapes(1).GroupItems.Item(1)但访问不会与InlineShapes.

有什么建议?

回答

1

如果你只有一组在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

+0

感谢@Vityata,我可以轻松地访问ActiveDocument.Shapes对象和里面GroupItems。主要问题是ActiveDocument.InlineShapes.GroupItem,它引发访问冲突 – fluffybunny