2

Is there a way to name text boxes in MS Word so i can call them in vba and add text to them?For Example,I have a User Form that gets some data from user.after some calculation on this data,i want to show the result on the document in text box or equation... Like,In visual basic we had :

textbox1.text="My Text Goes Here"

Do we have something like that in VBA? Like :

ActiveDocument.textboxname.text="My Text" ?!!

i dont want to use activeX Controls for this.

2 Answers 2

5

In Word and PowerPoint, you can only assign a name to a shape using VBA (in contrast to Excel where you can do this in the formula bar).

Word does not force shapes to have unique names, so it is possible to have two shapes both named Text Box 2. You can refer to shapes also by their index position in the ActiveDocument.Shapes collection.

Once you identify what Shape you need to work with, then you can simply manipulate the .TextFrame.TextRange.Text property:

Sub Test()
Dim shp As Shape
Dim str As String

For Each shp In ActiveDocument.Shapes
    str = "My name is " & shp.Name
    str = str & vbNewLine & "My EditID is " & shp.EditID
    shp.TextFrame.TextRange.Text = str
Next
End Sub

One other thing you might consider is adding an AlternativeText property to each shape. Of course this doesn't solve the "non-uniqueness" problem, but you can use this (or CustomerData/CustomXMLParts to assign some metadata to shapes, as a further means of identifying and differentiating them.

Sign up to request clarification or add additional context in comments.

1 Comment

Several years down the line I'm sharing a way of (now) giving names to textboxes in O365. Select outside the textbox, go to Layout and select the Selection Pane in the Arrange section. Shapes on that page are listed in the pane. Double-click an item to rename it.
2

There are few possibilities how you could take advantage of CC but, in my opinion, best option is to wrap each verified paragraph into CC.

Dim par As Paragraph

'set reference to appropriate paragraph
Set par = ActiveDocument.Paragraphs(2)

Dim cc As ContentControl
Set cc = ActiveDocument.ContentControls.Add( _
            wdContentControlRichText, par.Range)

cc.Tag = "VERIFIED"

'options
'disable deletion of CC
cc.LockContentControl = True

'disable edition of CC
cc.LockContents = True

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.