Most of us work with at least one Microsoft Word document that changes very little. For instance, you might send out a generic contract or proposal that updates client information and the fee structure, while the rest of the content remains the same. Relying on sight to manually modify the content that changes risks typos and missed updates. When you find yourself needing this type of document, consider using Word’s content controls to update dependent controls based on the results of another.
In this tutorial, I’ll show you how to populate a content control dropdown with class names. Within the body of the document, we’ll add three textbox context controls. When you choose a class from the dropdown, the dependent text controls will update with the class name, the professor and the student limit in the body of the document. You can easily modify the controls if necessary.
SEE: Windows, Linux, and Mac commands everyone needs to know (free PDF) (TechRepublic)
I’m using Microsoft 365 on a Windows 10 64-bit system, but you can use earlier versions down to Word 2010. Word for the web doesn’t support VBA.
What’s a content control in Word?
Content controls in Word are containers for content that let users build structured documents. A structured document controls where content appears within the document. You’ll use these types of documents when very little in the document changes from one use to another. By inserting content controls, you can change the few instances of content that need to change.
By connecting these controls, you can update them all with one control. When you do this, you create what’s known as dependent controls.
Content controls have many properties and are similar to the legacy controls but can be easier to work with and format. Similar to legacy controls, you can automate the control using VBA (Visual Basic for Applications), but you’ll use a VBA event procedure instead of a control property as you do with Word’s legacy controls.
The best way to learn about content controls is to use them, so that’s what we’ll do next.
How to add a dropdown content control in Word
All controls are available via Word’s Developer tab. As you can see in Figure A, there are several content controls and legacy controls are still available. I recommend using content controls when possible because legacy controls may disappear with little notice.
We’ll use the combo box content control dropdown to offer a few choices to the document user. After the user chooses an option from the dropdown (a class) and exits the dropdown, a VBA procedure will update three textbox content controls within the body of the document with the class and professor’s name and the student limit. You can add as many text controls as you need.
Now, to enter the dropdown, open a blank document and position the cursor where you want the dropdown. Click the Developer tab, and in the Controls group, click the Combo Box Content Control icon. Again in the Controls group, click Properties, and enter Class as the title.
To populate the dropdown, click Add in the Dropdown List Properties section. In the resulting dialog, enter Biology (Figure B), and click OK. Word automatically enters the same name for the Value setting. We won’t use this setting.
Select Add again in the Dropdown List Properties section, and repeat the process for both Anatomy and Physics (Figure C). Click OK.
Figure D shows the dropdown in the document. Next, we’ll create the body of the document, which will include three dependent text box content controls that will display the class by name, the professor and the student limit.
How to enter text box content controls in Word
The body of the document will have one sentence. Three text controls will make up the dependent content, combined with static content. Figure E shows the completed document with empty controls in design mode. You don’t have to work in design mode, but it’s easier to see the controls, the static content and the spaces between.
Next, let’s add the three controls that will update. Position the cursor a few lines below the dropdown. In the Controls group, click the Text Box Content Control icon to insert the first text control.
Click Properties in the Controls group and name the control ClassRepeat (Figure F). Click OK to close that dialog.
Press the spacebar and then enter the static content is taught by — be sure to enter a space after “by.”
Repeat this process, and name the second text control Professor. Follow the second text control with the static text and is limited to — again, adding spaces before “and” and after “to.”
Repeat this process again, and name the third text control Limit. Follow the third and last text control with the static text students. Adding a space before “students.”
At this point, your document should resemble the one shown earlier in Figure E. If you choose an item from the dropdown, nothing will happen to the three text controls. To update those controls, we’ll add a VBA procedure.
How to add a VBA procedure in Word
The document is ready, and the next step is to add the VBA procedure that uses the user’s choice in the dropdown, named Classes, to update the three text controls, named ClassRepeat, Professor and Limit.
Before you enter the procedure, save the file as a macro-enabled file. Then, enter Listing A by pressing Alt + F11 to open the Visual Basic Editor (VBE). In the Project Explorer to the left, double-click ThisDocument to open that module. You can enter the code manually or import the downloadable .cls file. In addition, the procedure is in the downloadable .docm file. If you enter the code manually, don’t paste from this web page. Instead, copy the code into a text editor and then paste that code into the ThisDocument module. Doing so will remove any phantom web characters that might otherwise cause errors.
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl,
Cancel As Boolean)
‘Text fields update automatically after the user chooses an item from the dropdown content control.
‘Dependent fields aren’t cleared between use.
On Error GoTo ErrHandler:
Dim objCC As ContentControl
For Each objCC In ActiveDocument.ContentControls
If objCC.Title = “Class” Then
Select Case objCC.Range.Text
ActiveDocument.SelectContentControlsByTitle(“ClassRepeat”).Item(1).Range.Text = “Biology”
ActiveDocument.SelectContentControlsByTitle(“Professor”).Item(1).Range.Text = “Professor Hoffman”
ActiveDocument.SelectContentControlsByTitle(“Limit”).Item(1).Range.Text = “15”
ActiveDocument.SelectContentControlsByTitle(“ClassRepeat”).Item(1).Range.Text = “Anatomy”
ActiveDocument.SelectContentControlsByTitle(“Professor”).Item(1).Range.Text = “Professor Douglas”
ActiveDocument.SelectContentControlsByTitle(“Limit”).Item(1).Range.Text = “10”
ActiveDocument.SelectContentControlsByTitle(“ClassRepeat”).Item(1).Range.Text = “Physics”
ActiveDocument.SelectContentControlsByTitle(“Professor”).Item(1).Range.Text = “Professor Collins”
ActiveDocument.SelectContentControlsByTitle(“Limit”).Item(1).Range.Text = “6”
Set objCC = Nothing
MsgBox Err.Number & ” ” & Err.Description
Set objCC = Nothing
After entering the procedure in the ThisDocument module, return to the document in Word. If the document is still in design mode, click Design Mode on the Developer tab to toggle out of design mode.
To run the procedure, choose an item from the Classes dropdown and press Tab to exit the control. Doing so will trigger the procedure in Listing A. As you can see in Figure G, the procedure updates the three text controls immediately.
When applying this technique to your own work, you’ll need to personalize everything. Updating a few instances of content within a document that remains mostly the same is easy when you use content controls in Word.