April 2006
Beginner
1114 pages
98h 16m
English
You can add text to any of the autoshapes using the Shape object’s TextFrame property. The TextFrame object provides a Characters property you can use to set the text displayed on the object, as well as formatting properties, as demonstrated by the following code:
Sub DrawText( )
Dim ws As Worksheet, s As Shape
Set ws = ActiveSheet
' Draw a rectangle.
Set s = ws.Shapes.AddShape(msoShapeRectangle, 10, 20, 100, 20)
' Add text.
s.TextFrame.Characters.text = "Some text to display"
' Center the text
s.TextFrame.HorizontalAlignment = xlHAlignCenter
s.TextFrame.VerticalAlignment = xlVAlignCenter
' Set the font
s.TextFrame.Characters.Font.Name = "Comic Sans MS"
s.TextFrame.Characters.Font.Bold = True
' Resize to fit text
s.TextFrame.AutoSize = True
End SubYou could substitute the AddTextBox method for AddShape in the preceding code; however, AddShape is more flexible since you can create any shape—not just rectangles.
Read now
Unlock full access