Creating Shapes

An AutoShape is a Shape object that represents a built-in drawing. To add a new AutoShape object, we use the AddShape method, whose syntax is:

ShapesObject.AddShape(Type, Left, Top, Width, Height)

The parameter Type is the type of AutoShape to create. It can be any one of the MsoAutoShapeType constants in Table A-1.

The required parameters Left and Top specify the position (in points as a Single) of the upper-left corner of the bounding box for the AutoShape object, measured relative to the upper-left corner of the container object (chart, chart sheet, or worksheet).

The Width and Height parameters specify the width and height (in points as a Single) of the bounding box for the AutoShape. Note that the type of a Shape object can be changed by setting the AutoShapeType property.

Table A-1. MsoAutoShapeType Constants (and Values)

msoShape16pointStar (94)

msoShapeFlowchartCard (75)

msoShapeLineCallout2BorderandAccentBar (122)

msoShape24pointStar (95)

msoShapeFlowchartCollate (79)

msoShapeLineCallout2NoBorder (118)

msoShape32pointStar (96)

msoShapeFlowchartConnector (73)

msoShapeLineCallout3 (111)

msoShape4pointStar (91)

msoShapeFlowchartData (64)

msoShapeLineCallout3AccentBar (115)

msoShape5pointStar (92)

msoShapeFlowchartDecision (63)

msoShapeLineCallout3BorderandAccentBar (123)

msoShape8pointStar (93)

msoShapeFlowchartDelay (84)

msoShapeLineCallout3NoBorder (119)

msoShapeActionButtonBackorPrevious (129)

msoShapeFlowchartDirectAccessStorage (87)

msoShapeLineCallout4 (112)

msoShapeActionButtonBeginning (131)

msoShapeFlowchartDisplay (88)

msoShapeLineCallout4AccentBar (116)

msoShapeActionButtonCustom (125)

msoShapeFlowchartDocument (67)

msoShapeLineCallout4BorderandAccentBar (124)

msoShapeActionButtonDocument (134)

msoShapeFlowchartExtract (81)

msoShapeLineCallout4NoBorder (120)

msoShapeActionButtonEnd (132)

msoShapeFlowchartInternalStorage (66)

msoShapeMixed (-2)

msoShapeActionButtonForwardorNext (130)

msoShapeFlowchartMagneticDisk (86)

msoShapeMoon (24)

msoShapeActionButtonHelp (127)

msoShapeFlowchartManualInput (71)

msoShapeNoSymbol (19)

msoShapeActionButtonHome (126)

msoShapeFlowchartManualOperation (72)

msoShapeNotchedRightArrow (50)

msoShapeActionButtonInformation (128)

msoShapeFlowchartMerge (82)

msoShapeNotPrimitive (138)

msoShapeActionButtonMovie (136)

msoShapeFlowchartMultidocument (68)

msoShapeOctagon (6)

msoShapeActionButtonReturn (133)

msoShapeFlowchartOffpageConnector (74)

msoShapeOval (9)

msoShapeActionButtonSound (135)

msoShapeFlowchartOr (78)

msoShapeOvalCallout (107)

msoShapeArc (25)

msoShapeFlowchartPredefinedProcess (65)

msoShapeParallelogram (2)

msoShapeBalloon (137)

msoShapeFlowchartPreparation (70)

msoShapePentagon (51)

msoShapeBentArrow (41)

msoShapeFlowchartProcess (61)

msoShapePlaque (28)

msoShapeBentUpArrow (44)

msoShapeFlowchartPunchedTape (76)

msoShapeQuadArrow (39)

msoShapeBevel (15)

msoShapeFlowchartSequentialAccessStorage (85)

msoShapeQuadArrowCallout (59)

msoShapeBlockArc (20)

msoShapeFlowchartSort (80)

msoShapeRectangle (1)

msoShapeCan (13)

msoShapeFlowchartStoredData (83)

msoShapeRectangularCallout (105)

msoShapeChevron (52)

msoShapeFlowchartSummingJunction (77)

msoShapeRegularPentagon (12)

msoShapeCircularArrow (60)

msoShapeFlowchartTerminator (69)

msoShapeRightArrow (33)

msoShapeCloudCallout (108)

msoShapeFoldedCorner (16)

msoShapeRightArrowCallout (53)

msoShapeCross (11)

msoShapeHeart (21)

msoShapeRightBrace (32)

msoShapeCube (14)

msoShapeHexagon (10)

msoShapeRightBracket (30)

msoShapeCurvedDownArrow (48)

msoShapeHorizontalScroll (102)

msoShapeRightTriangle (8)

msoShapeCurvedDownRibbon (100)

msoShapeIsoscelesTriangle (7)

msoShapeRoundedRectangle (5)

msoShapeCurvedLeftArrow (46)

msoShapeLeftArrow (34)

msoShapeRoundedRectangularCallout (106)

msoShapeCurvedRightArrow (45)

msoShapeLeftArrowCallout (54)

msoShapeSmileyFace (17)

msoShapeCurvedUpArrow (47)

msoShapeLeftBrace (31)

msoShapeStripedRightArrow (49)

msoShapeCurvedUpRibbon (99)

msoShapeLeftBracket (29)

msoShapeSun (23)

msoShapeDiamond (4)

msoShapeLeftRightArrow (37)

msoShapeTrapezoid (3)

msoShapeDonut (18)

msoShapeLeftRightArrowCallout (57)

msoShapeUpArrow (35)

msoShapeDoubleBrace (27)

msoShapeLeftRightUpArrow (40)

msoShapeUpArrowCallout (55)

msoShapeDoubleBracket (26)

msoShapeLeftUpArrow (43)

msoShapeUpDownArrow (38)

msoShapeDoubleWave (104)

msoShapeLightningBolt (22)

msoShapeUpDownArrowCallout (58)

msoShapeDownArrow (36)

msoShapeLineCallout1 (109)

msoShapeUpRibbon (97)

msoShapeDownArrowCallout (56)

msoShapeLineCallout1AccentBar (113)

msoShapeUTurnArrow (42)

msoShapeDownRibbon (98)

msoShapeLineCallout1BorderandAccentBar (121)

msoShapeVerticalScroll (101)

msoShapeExplosion1 (89)

msoShapeLineCallout1NoBorder (117)

msoShapeWave (103)

msoShapeExplosion2 (90)

msoShapeLineCallout2 (110)

msoShapeFlowchartAlternateProcess (62)

msoShapeLineCallout2AccentBar (114)

The short program in Example A-1 will display each AutoShape, along with its AutoShapeType, for 0.5 seconds. (It should be run on a blank worksheet. You can interrupt this program at any time by striking Ctrl-Break.) The Delay subroutine that it calls is shown in Example A-2.

Example A-1. Displaying Each AutoShape

Sub DisplayAutoShapes()

Dim sh As Shape
Dim i As Integer
Set sh = ActiveSheet.Shapes.AddShape(1, 100, 100, 72, 72)
For i = 1 To 138
   sh.AutoShapeType = i
   sh.Visible = True
   ActiveSheet.Cells(1, 1).Value = sh.AutoShapeType
   Delay 0.5
Next i

End Sub

Example A-2. The Delay Procedure

Public Sub Delay(rTime As Single)
'Delay rTime seconds (min=.01, max=300)
Dim OldTime As Variant
'Safty net
If rTime < 0.01 Or rTime > 300 Then rTime = 1
OldTime = Timer
Do
    DoEvents
Loop Until Timer - OldTime >= rTime
End Sub

Each Shape object has a text frame associated with it that holds any text associated with the object. The TextFrame property returns this TextFrame object.

The TextFrame object has a Characters property that returns a Characters collection. This collection can set the text in the text frame. For instance, the code in Example A-3 adds a rectangle to the active sheet and also adds text to the rectangle and sets the alignment for the text frame.

The FillFormat object sets various formatting for a Shape object. It is accessed using the Fill property of the Shape object. Among the properties of the FillFormat object are the BackColor, ForeColor, Pattern, and Visible properties. To set one of the color properties, we use the RGB color model, as in the following example:

sh.Fill.ForeColor.RGB = RGB(0, 0, 255)

To illustrate the use of AutoShapes, Example A-4 inserts a dampened sine curve of small stars in the drawing layer.

The output from this code is shown in Figure A-3.

The code in Example A-5 produces a random series of stars, each containing a single letter that together spells a name. Note that each time the program is run, the pattern is different.

The output from this code is shown in Figure A-4.

Example A-6 prints a hypocycloid. (It may take a few minutes to complete.)

The results are shown in Figure A-5. (The small vertical bar in Figure A-5 indicates the left edge of the active cell.