Microsoft Word Content Controls VBA (Visual Basic for Applications)

Microsoft Word Content Controls VBA (Visual Basic for Applications)

Content Controls

In-line user controls which can be embedded in the document withing a range or selection. Microsoft Word VBA object offers ten Content Controls listed below which can be short hand the programming and provide various user input methods:

Syntax

expression.ContentControls.Add(Type)

List Content Controls

  1. wdContentControlBuildingBlockGallery
  2. wdContentControlCheckBox
  3. wdContentControlComboBox
  4. wdContentControlDate
  5. wdContentControlDropdownList
  6. wdContentControlGroup
  7. wdContentControlPicture
  8. wdContentControlRepeatingSection
  9. wdContentControlRichText
  10. wdContentControlText

Example

Following code will create three labels and three controls to take user input on a blank document. Note to arrange multiple controls I have used a special symbol to avoid overlapping of the control on the page which I seek through MoveUntil method by passing it in Cset parameter.

RichTextBox Control

Public Sub AddRichTextContentControl()
On Error GoTo errh
    Dim objContentControl As ContentControl
    Dim oDocument As Document
    
    
    'Bind active document reference
    Set oDocument = ActiveDocument
    
    'Type blank
    Selection.Text = "|"
    
    'Move selection at the start
    Selection.Collapse Direction:=WdCollapseDirection.wdCollapseStart
    
    'Populate label
    Selection.Text = "Enter name : "
    
    'Move selection at the end
    Selection.MoveUntil cset:="|"
    
    'Add textbox
    Set objContentControl = Selection.ContentControls.Add(Type:=wdContentControlRichText)
    

errh:
    'Memory cleanup
    If Not objContentControl Is Nothing Then
        Set objContentControl = Nothing
    End If
    If Not oDocument Is Nothing Then
        Set oDocument = Nothing
    End If
    
If Err.Number <> 0 Then
End If
End Sub

Output

ComboBox Control

Public Sub ComboBoxAddContentControl()
On Error GoTo errh
    Dim objContentControl As ContentControl
    Dim oDocument As Document
    
    
    'Bind active document reference
    Set oDocument = ActiveDocument
    
    'Type blank
    Selection.Text = "|"
    
    'Move selection at the start
    Selection.Collapse Direction:=WdCollapseDirection.wdCollapseStart
    
    'Populate label
    Selection.Text = "Select a color : "
    
    'Move selection at the end
    Selection.MoveUntil cset:="|"
    
    'Bind control by creating
    Set objContentControl = Selection.ContentControls.Add(Type:=wdContentControlComboBox)
    
    'Add some items in combobox
    With objContentControl
        .DropdownListEntries.Add "RED"
        .DropdownListEntries.Add "YELLOW"
        .DropdownListEntries.Add "GREEN"
        .DropdownListEntries.Add "BLACK"
        .DropdownListEntries.Add "WHITE"
    End With
    
    
errh:
    'Memory cleanup
    If Not objContentControl Is Nothing Then
        Set objContentControl = Nothing
    End If
    If Not oDocument Is Nothing Then
        Set oDocument = Nothing
    End If
    
If Err.Number <> 0 Then
End If
End Sub

Output

Date Control

Public Sub DateAddContentControl()
On Error GoTo errh
    Dim objContentControl As ContentControl
    Dim oDocument As Document
    
    
    'Bind active document reference
    Set oDocument = ActiveDocument
    
    'Type blank
    Selection.Text = "|"
    
    'Move selection at the start
    Selection.Collapse Direction:=WdCollapseDirection.wdCollapseStart
    
    
    'Populate label
    Selection.Text = "Select Date : "
    
    'Move selection at the end
    Selection.MoveUntil cset:="|"
    
    'Bind control by creating
    Set objContentControl = Selection.ContentControls.Add(Type:=wdContentControlDate)
errh:
    'Memory cleanup
    If Not objContentControl Is Nothing Then
        Set objContentControl = Nothing
    End If
    If Not oDocument Is Nothing Then
        Set oDocument = Nothing
    End If
    
If Err.Number <> 0 Then
End If
End Sub

Output

Similarly you can modify code to populate rest of the control in running document commentary. It can build a simple data entry form to get user inputs.

Leave a Reply

Your email address will not be published.