CanvasShapes Collection

Microsoft Word Visual Basic

CanvasShapes Collection

         
Multiple objects CanvasShapes
Multiple objects

Represents the shapes in a drawing canvas.

Using the CanvasShapes collection

Use the CanvasItems property of either a Shape or ShapeRange object to return a CanvasShapes collection. To add shapes to a drawing canvas, use the following methods of the CanvasShapes collection: AddCallout, AddConnector AddCurve, AddLabel, AddLine, AddPicture, AddPolyline, AddShape, AddTextbox, AddTextEffect, or BuildFreeForm. The following example adds a drawing canvas to the active document and then adds three shapes to the drawing canvas.

Sub AddCanvasShapes()
    Dim shpCanvas As Shape
    Dim shpCanvasShapes As CanvasShapes
    Dim shpCnvItem As Shape

    'Adds a new canvas to the document
    Set shpCanvas = ActiveDocument.Shapes _
        .AddCanvas(Left:=100, Top:=75, _
        Width:=50, Height:=75)
    Set shpCanvasShapes = shpCanvas.CanvasItems

    'Adds shapes to the CanvasShapes collection
    With shpCanvasShapes
        .AddShape Type:=msoShapeRectangle, _
            Left:=0, Top:=0, Width:=50, Height:=50
        .AddShape Type:=msoShapeOval, _
            Left:=5, Top:=5, Width:=40, Height:=40
        .AddShape Type:=msoShapeIsoscelesTriangle, _
            Left:=0, Top:=25, Width:=50, Height:=50
    End With
End Sub

Use CanvasItems(index), where index is the name or the index number, to return a single shape in the CanvasShapes collection. The following example sets the Line and Fill properties and vertically flips the third shape in a drawing canvas.

Sub CanvasShapeThree()
    With ActiveDocument.Shapes(1).CanvasItems(3)
        .Line.ForeColor.RGB = RGB(50, 0, 255)
        .Fill.ForeColor.RGB = RGB(50, 0, 255)
        .Flip msoFlipVertical
    End With
End Sub

Each shape is assigned a default name when it is created. For example, if you add three different shapes to a document, they might be named Rectangle 2, TextBox 3, and Oval 4. Use the Name property to reference the default name or to assign a more meaningful name to a shape.