VBA for Excel 2007 tutorial-add text to shape


VBA code to add text to shape

In the previous page you learnt to insert shapes in to Excel sheet by using VBA code. In this example, you will be able to add text to the shape. To display text on the shape, you can use the TextFrame2 object property of the shape object. This property comes with the TextRange property that allows you to add text to the shape.

To run this example VBA code, you will need a Form, and a CommandButton.



VBA for Excel: vba example code to add text to the shape object
VBA code for this example:


Private Sub CommandButton1_Click()
   'Add a a simple heart shape in Excel worksheet
  Worksheets(5).Shapes.AddShape msoShapeHeart, 50, 50, 200, 200
  With Worksheets(5).Shapes(1)
       .Fill.ForeColor.RGB = RGB(100, 255, 0) 'Set forecolor of the shape
       .Line.DashStyle = msoLineSolid 'Specify line type
       .TextFrame2.TextRange.Text = "Sample text" ' Text to display on the shape
   End With


End Sub

Comments





This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.