I am trying to VBA my way into a content control drop down where I can select from the list of Display Name content, but on exit, the text that is actually put into the document is the Value content. For example if I select Shannon Thomson from the list, the document will then read ST where the content control is positioned.
There isn't a lot of examples for this particular function that I have found, and none of them have worked. I am really new at VBA and not sure how to learn to get better because it seems like so few people are even using it, so am relying on good old copy paste at the moment.
So far I have tried this:
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
and nothing happened. Also tried this:
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
That made a popup box with the value text inside it open, but nothing else.
Also tried this one:
Option Explicit
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Dim i As Long, StrDetails As String
With ContentControl
If .Title = "Client" Then
For i = 1 To .DropdownListEntries.Count
If .DropdownListEntries(i).Text = .Range.Text Then
StrDetails = Replace(.DropdownListEntries(i).Value, "|", Chr(11))
Exit For
End If
Next
ActiveDocument.ContentControls(2).Range.Text = StrDetails
End If
End With
End Sub
That one yelled at me about "The Requested member of the collection does not exist" and highlighted the 4th row from the bottom.
This one, and nothing happened at all
Sub GetSelectedValue()
Dim cc As ContentControl
Set cc = ActiveDocument.SelectContentControlsByTitle("YourComboBoxTitle").Item(1)
If cc.Type = wdContentControlComboBox Then
Dim selectedValue As String
selectedValue = cc.Range.Text
Use the selectedValue as needed
End If
End Sub
There's also this one:
Private Sub ComboBox1_Change()
Dim fullName As String
Dim initials As String
fullName = ComboBox1.Text
initials = GetInitials(fullName)
ActiveDocument.SelectContentControlsByTitle("ComboBox1").Item(1).Range.Text = initials
End Sub
Function GetInitials(fullName As String) As String
Dim nameParts() As String
Dim i As Integer
nameParts = Split(fullName)
GetInitials = ""
For i = LBound(nameParts) To UBound(nameParts)
GetInitials = GetInitials & Left(nameParts(i), 1)
Next i
End Function
That one the bottom End Function line turns red and gets a question mark at the end.
I have no idea what I am doing wrong guys can someone help me make this daffy thing work?
Note: The code only works for Combo Box Content Control
.
Microsoft documentation:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
Dim oDLE As ContentControlListEntry
With CCtrl
If .Type = wdContentControlComboBox Then
For Each oDLE In .DropdownListEntries
If oDLE.Text = .Range.Text Then
.Range.Text = oDLE.Value
Exit For
End If
Next
End If
End With
End Sub