Reputation: 25
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.
Upvotes: 2
Views: 29590
Reputation: 53663
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.
Upvotes: 5
Reputation: 4445
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
Upvotes: 2