jdcarroll15
jdcarroll15

Reputation: 13

how to get dropdown value (not display text) from word content control using VBA

I have a drop-down content control element in my word doc. When I look at the properties of that content control, they define the "Display Name" and a "Value". I've found VBA code that shows how to grab the display text/name, but I cannot find anything that shows how to get the value information using VBA.

enter image description here

I basically want to pull the "value" field from this, and a couple of other drop-downs, to fill in a text field via VBA.

Upvotes: 1

Views: 4213

Answers (2)

Charles Kenyon
Charles Kenyon

Reputation: 1028

Here is a function based on Paul's code above.

Function CCValue(ccMyContentControl As ContentControl) As String
    ' Charles Kenyon 29 September 2020
    '
    ' Function based on Paul Edstein's Code
    ' https://stackoverflow.com/questions/58809271/how-to-get-dropdown-value-not-display-text-from-word-content-control-using-vba
    '
    If ccMyContentControl.Type <> wdContentControlDropdownList Then
        If ccMyContentControl.Type <> wdContentControlComboBox Then
            CCValue = ccMyContentControl.range.Text
            Exit Function
        End If
    End If
    Dim i As Long
    With ccMyContentControl
        For i = 1 To .DropdownListEntries.Count
            If .DropdownListEntries(i).Text = .range.Text Then _
                CCValue = .DropdownListEntries(i).Value
        Next i
    End With
End Function

When provided the Content Control, it returns the value in the case of a dropdown or combobox CC and the text on any other Content Control.

Upvotes: 0

macropod
macropod

Reputation: 13505

Try:

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

Upvotes: 2

Related Questions