只能为组VBA Word文档访问此成员

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了只能为组VBA Word文档访问此成员相关的知识,希望对你有一定的参考价值。

我试图在Word文档中找到ole对象,它似乎在InlineShapes(1).GroupItems。但是无法访问组项,因为它给了我错误。

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.

有什么建议?

答案

如果在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

以上是关于只能为组VBA Word文档访问此成员的主要内容,如果未能解决你的问题,请参考以下文章