Adding text to Text box

M

M.L.Srinivas

Hi,

How can we add text to a text box(not a form field) drawn
(using insert>textbox) on a word document using vba.

Any kind of help is welcome.

Thanks

M.L.Srinivas
 
P

Peter Hewett

Hi mlsrinivas

I'm assuming you want to update TextBoxes that already exist, not create new
TextBoxes. Here's some code that should help:

Public Sub AddTextToTextBox()
Dim shrItem As Word.Shape
Dim lngShape As Long

' Iterate all Shapes in the ShapeRange object
With ActiveDocument.Range.ShapeRange
For lngShape = 1 To .Count
Set shrItem = .Item(lngShape)

' We're only interested in TextBoxes
If shrItem.Type = msoTextBox Then
shrItem.TextFrame.TextRange.Text = "hello world"
End If
Next
End With
End Sub

Note: Shapes can be grouped, this code wont update a grouped TextBox. You may
need to identify a specific TextBox (otherwise how do you know which TextBox
you're stuffing text into?). The easiest way to do this is to set the shapes
name, something like this:

ActiveDocument.Range.ShapeRange(1).Name = "TB_Name"

HTH + Cheers - Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top