2017-12-18 433 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