Search code examples
vbams-wordcombobox

How get selected value from drop-down list (Content control) using VBA in Word


I am new doing VBA in word, but use quite a lot in excel. I have a Content Control Combo box in my word document. I have names of employees to choose from, and the value I have set to their email address. The idea is to send an email address to the person chosen from the combo box, by selecting a check box. I now how to do code to send the email when selecting the checkbox, but I am struggling with getting the Value (ie email address) from the combo box. All I want is a code that gives me the selected value from my combo box named "Reviewers". I need help with all the code, including how to DIM.

enter image description here


Solution

  • For example, as a ContentControlOnExit macro that runs directly from your 'Reviewers' dropdown:

    Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
    Dim i As Long, StrEmail As String
    With ContentControl
      If .Title = "Reviewers" Then
      For i = 1 To .DropdownListEntries.Count
        If .DropdownListEntries(i).Text = .Range.Text Then
          StrEmail = .DropdownListEntries(i).Value
          MsgBox StrEmail 
          Exit For
        End If
      Next
      End If
    End With
    End Sub
    

    Alternatively, as a ContentControlOnExit macro that runs directly from a Checkbox titled 'Send Email':

    Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
    Dim i As Long, StrEmail As String
    With CCtrl
      If .Title = "Send Email" Then
        If .Checked = True Then
          With .Parent.SelectContentControlsByTitle("Reviewers")(1)
            For i = 1 To .DropdownListEntries.Count
              If .DropdownListEntries(i).Text = .Range.Text Then
                StrEmail = .DropdownListEntries(i).Value
                MsgBox StrEmail
                Exit For
              End If
            Next
          End With
        End If
      End If
    End With
    End Sub