The Anchorage
Personal website of Gregory K. Maxey, Commander USN (Retired)
The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.
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!
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.
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.
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.
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.
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.
The "magic" is accomplished by:
The VBA required to perform these steps is shown here:
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
Note: The VBA script above is placed in the "ThisDocument" module "Document" pane of the document or template VB project.
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.
When the user selects a displayed dropdown list item and exits the content control the content control displays the defined value.
The "magic" is accomplished by:
The VBA required to perform these steps is shown here:
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
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:
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.
Note: 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.
The techniques demonstrated above can also be employed to enhance combo box content controls.
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
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
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.
The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.
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!