Attaching vba to OnExit of Drop-down list box Content Control in Microsoft Word latest version

Sally Green 0 Reputation points
2025-05-26T07:17:45.1166667+00:00

I've used vba with the Legacy controls for many years but now need to attach code to identify an item selected in a drop down list box - Content Control. There are two drop-down list box content controls in the template. One has the title: Policy, the other has the title: Group. I am going to need to assign code to move to bookmarks and insert auto-text based on the choices made in each of these drop-down list boxes.

I've tried:

Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)

On Error GoTo EH

Dim objCC As ContentControl

For Each objCC In ActiveDocument.ContentControls

    If objCC.Title = "Group" Then

        Select Case objCC.Range.Text

            Case "Apple"

                MsgBox "Applewas selected"

            Case "Orange"

                MsgBox "Orangewas selected"

            Case "Pear"

                MsgBox "Pear was selected"

        End Select

    End If

Next objCC

    

Set objCC = Nothing

Exit Sub

EH:

MsgBox Err.Number & " " & Err.Description

Set objCC = Nothing

End Sub

No code runs when I exit the content control. I've also tried my Private Sub being called this, with all remaining code the same.

Private Sub Document_ContentControlOnExit(ByVal objCC As ContentControl, Cancel As Boolean)

I would be grateful for any help you can offer.

Many thanks

SG

Microsoft 365 and Office | Word | For business | Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jay Freedman 201 Reputation points Volunteer Moderator
    2025-05-27T23:50:35.3166667+00:00

    The code looks like it should work, assuming that each dropdown currently shows one of the three choices in the Cases.

    It isn't obvious whether you placed the code in the ThisDocument module of either the document containing the controls or the template that's attached to the document. It should not be in any "regular" module.

    When you select the ThisDocument module in the Project pane, you can choose Document in the dropdown at the top left of the code panel. Then the available event handlers are listed in the dropdown at the top right. Besides ensuring that the code is in the correct module, this procedure automatically enters the correct Sub statement with all its parameters.

    1

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.