2

I have created a Visual Basic for Applications program in Microsoft Word (2016, 64bit, Windows 10)

It consists of a UserForm with about 30 TextBoxes and a submit button. Each TextBox has a unique name and tag.

I also have the same number of ContentControl text boxes in the word document, each of them have the same title and tag as its corresponding UserForm TextBox.

What I'm after is a better way to populate the document text boxes from the UserForm TextBoxes when the user clicks submit.

I currently am doing this by typing 3 lines of code for each TextBox, but this is tedious. I copy and paste the 3 lines of code each time but must edit a small part of each line every time.

I thought about using a loop, as shown in the code below, where I use a variable x to copy the TextBox across by name, but I'm unsure how to proceed.

Dim doc As Document
Dim ccs As ContentControls
Dim cc As ContentControl
Set doc = ActiveDocument

For … 
' Somehow find each text box, put the name of one into variable x then

    Set ccs = doc.SelectContentControlsByTag(x)
    Set cc = ccs(1)
    cc.Range.Text = x.Text

Next

Thank you in advance!

robinCTS
  • 5,746
  • 14
  • 30
  • 37
James
  • 1,928
  • 3
  • 13
  • 30

2 Answers2

2

The UserForm TextBoxes are accessible via the Controls class, whilst the document ContentControl text boxes are accessible via the ContentControls property or the SelectContentControlsByTag function.

There is no need to specially name the text boxes as they are easily discernible by type.


One solution is to loop through the appropriate ContentControls and copy by name/title:

Private Sub cmdSubmit_Click()

  Dim cc As Word.ContentControl

  For Each cc In Word.ActiveDocument.ContentControls
    If cc.Type = Word.WdContentControlType.wdContentControlText Then
      cc.Range.Text = Me.Controls(cc.Title).Text
    End If
  Next cc

End Sub

If you are using rich text ContentControls, then you need to substitute wdContentControlText with wdContentControlRichText.


Another solution is to loop through the appropriate TextBoxes and copy by tag:

Private Sub cmdSubmit_Click()

  Dim doc As Word.Document
  Dim cc As Word.ContentControl
  Dim c As MSForms.Control

  Set doc = Word.ActiveDocument
  For Each c In Me.Controls
    If TypeName(c) = "TextBox" Then
      Set cc = doc.SelectContentControlsByTag(c.Tag)(1)
      cc.Range.Text = c.Text
    End If
  Next c

End Sub
robinCTS
  • 5,746
  • 14
  • 30
  • 37
  • I just rewrote the question. I hope it is better! If you think it is and you want to could you please upvote it! Thanks! I already figured it out but wanted to make it better in case someone else needs help! – James Sep 10 '17 at 19:02
1

All the TextBoxes are stored in a class of Shapes, which you can access like this:

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

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

You need to name your textboxes so that you can identify them as textboxes, then you can check to see if they are textboxes before you write to them.

Rob Anthony
  • 1,743
  • 1
  • 13
  • 17
  • I just rewrote the question. I hope it is better! If you think it is and you want to could you please upvote it! Thanks! You already helped me figured it out but I wanted to make it better in case someone else needs help in the future! Also thanks for saving me so many hours! – James Sep 10 '17 at 19:01