Name

shapes.AddTextbox(Orientation, Left, Top, Width, Height)

Synopsis

Draws a text box surrounded by a rectangular border and returns the text box’s Shape object.

Argument

Settings

Orientation

An msoTextOrientation constant. Can be msoTextOrientationDownward, msoTextOrientationHorizontal, msoTextOrientationHorizontalRotatedFarEast, msoTextOrientationMixed, msoTextOrientationUpward, msoTextOrientationVertical, or msoTextOrientationVerticalFarEast.

Left

The horizontal position of the shape in points.

Top

The vertical position of the shape in points.

Width

The width of the shape in points.

Height

The height of the shape in points.

Use the TextFrame property to get or set the text in the shape and to set the formatting of the text. Unlike labels, text boxes do not automatically resize to fit their text. You must set the AutoSize property as shown here:

Sub DrawTextbox( )
    Dim ws As Worksheet, s As Shape
    Set ws = ActiveSheet
    ' Create label (height/width will be set by AutoSize).
    Set s = ws.Shapes.AddTextbox(msoTextOrientationHorizontal, 100, 100, 1, 1)
    s.TextFrame.Characters.text = "This is some label text"
    ' Resize text box to fit text.
    s.TextFrame.AutoSize = True
End Sub

Get Programming Excel with VBA and .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.