Resting Anchor

The Anchorage

Personal website of Gregory K. Maxey, Commander USN (Retired)

Content Control ADCAP (Advanced Capability) Dropdown Lists
(A Microsoft Word Help & Tip page by Gregory K. Maxey)

DISCLAIMER/TERMS OF USE

The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.

Click to acces PayPal Verification Service Click to acces PayPal Verification Service

However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.

If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!

Click to donate British Pound Sterling                   Click to donate US dollars                   Click to donate EU euros

This Microsoft Word Tips & Microsoft Word Help page demonstrates some techniques that you can employ to add "magic" to and maximize the usefulness of content control dropdown lists and combo boxes.

Basic Dropdown Lists

When you create and define a content control dropdown list in your document, your document users are limited to selecting only one of the defined list members.

For example, in the dropdown list shown below the document user must select "A, B or C" or make no selection at all.

magic_dropdown_1

That is useful in many cases sure, but what if you need your user to select a listed value but then have the control display a different related value?

With the addition of some simple VBA in the Document_ContentControlOnExit event, you can make your dropdown list or combo box content controls appear to do "magic" and significantly expand the usefulness of these type controls.  Hereafter, I will refer to these "magic" controls as "ADCAP" (or Advanced Capability) content controls.

ADCAP Dropdown List

Display Calculated Value (Ordinal numbers)

Let's consider the case where the user is asked to select a number (e.g., day of the month) from a dropdown list and you want the numbered selected to be displayed in ordinal format.

Content control dropdown list members can consist of numbers but the list members themselves can't be a mix of numbers and superscript text.

For example, you can't define 1st, 2nd, 3rd, 4th, etc. as the list members.  The following illustrates a portion of a content control dropdown list containing the numbers.  The actual numbers defined are 1 to 31.

magic_dropdowns_2

After the user selects a listed number and exits the content control, a VBA script in the Document_ContentControlOnExit event converts the selected number to the ordinal value of that number and displays it in the content control.

magic_dropdowns_3

 The "magic" is accomplished by:

The VBA required to perform these steps is shown here:

VBA Script:
Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl, Cancel As Boolean)
Dim oRng As Range
Dim lngVal As Long
  Select Case CC.Title
    Case "Date Ordinal"
      With CC
        If .ShowingPlaceholderText Then Exit Sub
        If IsNumeric(.Range.Text) Then
          .Type = wdContentControlRichText
          Set oRng = .Range
          With oRng
            lngVal = CLng(.Text)
            .Start = .End
            .InsertAfter Ordinal(lngVal)
            .Font.Superscript = True
          End With
          .Type = wdContentControlDropdownList
        End If
      End With
  End Select
lbl_Exit:
  Exit Sub
End Sub

Function Ordinal(lngNum As Long) As String
'Adapted from function posted Microsoft Answers by Paul Edstein.
Dim strOrd As String
  '11, 12, and 13 numbers including one of them e.g., 112, 512 etc. _
  are the only numbers ending 1, 2 or 3 where the ordinal is th instead of st, nd or rd.
  If (lngNum Mod 100) < 11 Or (lngNum Mod 100) > 13 Then
    Select Case lngNum Mod 10
      Case 1
        strOrd = "st"
      Case 2
        strOrd = "nd"
      Case 3
        strOrd = "rd"
      Case Else
        strOrd = "th"
    End Select
  End If
  Ordinal = IIf(strOrd = vbNullString, "th", strOrd)
lbl_Exit:
  Exit Function
End Function

Site Note IconNote:  The VBA script above is placed in the "ThisDocument" module "Document" pane of the document or template VB project.

Display Defined Value

You can use a similar technique to display the dropdown list item's defined "value" after the user makes a selection and exits the control.

For example, the following illustrates a content control with its defined display name and value properties.

magic_dropdowns_4

magic_dropdowns_5

When the user selects a displayed dropdown list item and exits the content control the content control displays the defined value.

magic_dropdowns_6

The "magic" is accomplished by:

The VBA required to perform these steps is shown here:

VBA Script:
Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl, Cancel As Boolean)
Dim lngIndex As Long
Dim strValue As String
  Select Case CC.Title
    Case "Magic Dropdown"
      If CC.ShowingPlaceholderText Then Exit Sub
      With CC
        For lngIndex = 2 To .DropdownListEntries.Count
          If .DropdownListEntries(lngIndex).Text = .Range.Text Then
            strValue = .DropdownListEntries(lngIndex).Value
            .Type = wdContentControlText
            .Range.Text = strValue
            .Type = wdContentControlDropdownList
            Exit For
          End If
        Next lngIndex
      End With
    Case Else
  End Select
lbl_Exit:
  Exit Sub
End Sub

Display a Pre-defined Building Block

For larger blocks of pre-defined text you can use a similar technique to display a "text" building block in the the dropdown list after the user makes a selection and exits the control.

The following content control illustrates this technique:

magic_dropdowns_7

magic_dropdowns_8

The VBA code for the preceding example is included in the demonstration template that you can download using the link at the bottom of this tips page.

Site Note IconNote:  There is a limitation when defining building blocks to use with this method.  The building block defined may contain multiple lines but it "must" consist of a single paragraph unit.  An example is included in the demonstration template.

ADCAP Combo Box

The techniques demonstrated above can also be employed to enhance combo box content controls.

Display Calculated Value (Spell out currency)

The content control shown below is a combo box type control listing the numerical values $1.00 to $100.00.  Unlike dropdown lists content controls, the document user may select a listed value or enter any text value in the combo box control.

In this example the user enters $3.98 

magic_dropdown

magic_dropdowns_10

magic_dropdowns_11

Dynamic ADCAP Dropdown List

It gets even better! By applying a relatively simple VBA range monitor you can create a custom Content Control OnChange event.

Using the OnChange event, you can create ADCAP dropdown lists that are truly dynamic.  Unlike the previous examples, there is no need for the user to exit the control before the alternate text is displayed.

The following video illustrates a dynamic ADCAP dropdown list:

Other examples and the code for creating dynamic ADCAP dropdown lists are included in the demonstration template package.

For more on content control custom events see my:  Content Control Custom Events

Conclusion

I would like to acknowledge and thank Doug Robbins and Paul Edstein (aka macropod) for their code contributions to the examples shown here or in the supporting template.  Doug created the basic code for converting currency to text.  Paul created the basic code for converting ordinals.  Additionally, and he may not have realized it at the time, he is the one that discovered the trick of temporarily redefining the content control type which is the Key to the Kingdom and necessary to make it all work! 

That's it! I hope you have found this tips page useful and informative. You can download the template I used with the examples demonstrated here:  Demo Template Package.

Share

DISCLAIMER/TERMS OF USE

The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.

Click to acces PayPal Verification Service Click to acces PayPal Verification Service

However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.

If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!

Click to donate British Pound Sterling                   Click to donate US dollars                   Click to donate EU euros

Search my site or the web using Google Search Engine

Google Search Logo