Skip to main content

Shape object

Table of contents
  1. Remarks
  2. Example

Represents an object in the drawing layer, such as an AutoShape, freeform, OLE object, or picture.

Remarks

The Shape object is a member of the Shapes collection. The Shapes collection contains all the shapes in a workbook.

To return... Use...
A Shape object that represents one of the shapes attached by a connector The BeginConnectedShape or EndConnectedShape property of the ConnectorFormat object.
A newly created freeform The BuildFreeform and AddNodes methods to define the geometry of a new freeform, and use the ConvertToShape method to create the freeform and return the Shape object that represents it.
A Shape object that represents a single shape in a grouped shape GroupItems (index), where index is the shape name or the index number within the group.
A newly formed group of shapes The Group or Regroup method of the ShapeRange object to group a range of shapes and return a single Shape object that represents the newly formed group. After a group has been formed, you can work with the group the same way that you work with any other shape.
A Shape object that represents an existing shape Shapes (index), where index is the shape name or the index number.
A Shape object that represents a shape within the selection Selection.ShapeRange (index), where index is the shape name or the index number.

Example

The following example horizontally flips shape one and the shape named Rectangle 1 on myDocument.

Set myDocument = Worksheets(1) 
myDocument.Shapes(1).Flip msoFlipHorizontal 
myDocument.Shapes("Rectangle 1").Flip msoFlipHorizontal

Each shape is assigned a default name when you add it to the Shapes collection. To give the shape a more meaningful name, use the Name property. The following example adds a rectangle to myDocument, gives it the name Red Square, and then sets its foreground color and line style.

Set myDocument = Worksheets(1)
With myDocument.Shapes.AddShape(msoShapeRectangle, 144, 144, 72, 72)
    .Name = "Red Square"
    .Fill.ForeColor.RGB = RGB(255, 0, 0)
    .Line.DashStyle = msoLineDashDot
End With

The following example sets the fill for the first shape in the selection in the active window, assuming that there's at least one shape in the selection.

ActiveWindow.Selection.ShapeRange(1).Fill.ForeColor.RGB = RGB(255, 0, 0)

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>