Share via


Programmatically Working with Content Controls in Word 2010

Office Quick Note banner

Handy Programming Tips for Microsoft Word 2010: Learn how to create and populate a content control in Microsoft Word 2010.

Applies to: Office 2010 | VBA | Word 2010

In this article
Add the Code to the Visual Basic Editor
Test the Solution
Next Steps

Published:   June 2011

Provided by:    Frank Rice, Microsoft Corporation

Content controls provide a way for you to design documents and templates that have a user interface, implement data binding to a data source, and restrict users from editing protected sections of a document or template. In this topic, you programmatically create a combo box content control and then populate it. To complete this task, you must do the following:

  • Add the Code to the Visual Basic Editor

  • Test the Solution

Add the Code to the Visual Basic Editor

In this task, you add programming code to the Visual Basic Editor.

To add code to the Visual Basic Editor

  1. Start Word.

  2. On the Developer tab, click Visual Basic. This opens the Visual Basic Editor.

    Note

    If you do not see the Developer tab in Word 2010, click the File tab, and then click Options. In the categories pane, click Customize Ribbon, select Developer, and then click OK.

  3. In the Project pane, click ThisDocument.

  4. Paste or type the following Microsoft Visual Basic for Applications (VBA) code into the module window.

    Sub SetPlaceholderText()
        ' Delete all the current contents of the document.
        Range.Delete
    
        ' Create a combo box content control:
        Dim ccList As ContentControl
        Set ccList = ContentControls.Add(wdContentControlComboBox)
    
        ccList.Title = "Select a Color"
        ccList.SetPlaceholderText Text:="Please select the color"
    
        ' Add items to the list:
        ccList.DropdownListEntries.Add "Red", RGB(255, 0, 0)
        ccList.DropdownListEntries.Add "Orange", RGB(255, 165, 0)
        ccList.DropdownListEntries.Add "Yellow", RGB(255, 255, 0)
        ccList.DropdownListEntries.Add "Green", RGB(0, 255, 0)
        ccList.DropdownListEntries.Add "Blue", RGB(0, 0, 255)
        ccList.DropdownListEntries.Add "Indigo", RGB(75, 0, 130)
        ccList.DropdownListEntries.Add "Violet", RGB(238, 130, 238)
    
        ' Display the contents of each item:
        Dim ccListEntry As ContentControlListEntry
        For Each ccListEntry In ccList.DropdownListEntries
            Debug.Print ccListEntry.Text, Hex$(ccListEntry.Value)
        Next ccListEntry
    
        ' Select the first item in the list.
        ccList.DropdownListEntries(1).Select
    End Sub
    

Test the Solution

In this task, you run the VBA code that creates the combo box content control and populates it with a list of colors.

To run the code

  • In the Visual Basic Editor, place your cursor in the SetPlaceholderText subroutine and then press F5. Examine the Immediate window for the results. Also examine the document which will contain a combo box content control displaying a list of colors.

Next Steps