#1
|
|||
|
|||
Content Control values NOT display name
HI
I have two content controls: CC1 and CC2 in separate tables CC1 is a dropdown showing the display name: Display name Value meets 0.25 exceeds 0.75 I would like CC2 to show the value such that if CC1=meets, then CC2=0.25 I've tried the REF function and VBA to set the value but I can't get it to work, it seems the control created with ref takes the name of the control its referencing. Right now I'm exploring nested IF statements...these look messy! any help is appreciated thanks |
#2
|
||||
|
||||
The simplest way is to use a ContentControlOnExit macro, coded along the lines of:
Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean) Dim StrOut As String With CCtrl If .Title = "CC1" Then If .Range.Text = "meets" Then StrOut = "0.25" Else StrOut = "" End If ActiveDocument.SelectContentControlsByTitle("CC2")(1).Range.Text = StrOut End If End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Thank you for such a quick reply. I'm having difficulty, the code stops on:
ActiveDocument.SelectContentControlsByTitle("CC2") (1).Range.Text = StrOut With the runtime error 438 object does not support this property or method. Currently CC2 is a combo with no values? I've tried different CCtrls: rich text, plain text and dropbox, none have worked. What content control should I be using? thanks again |
#4
|
||||
|
||||
Your CC2 should be a text content control or a rich text content control - both of which support the code I posted.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Late to the party, but this thread shows up in Google searches. I thought to add another method.
Code:
Sub DropDownValue() ' A scratch macro by Charles Kenyon 29 Sept 2020 ' Assigns value of a DropDown Content Control to a string and reports in message box ' https://answers.microsoft.com/en-us/msoffice/forum/all/use-value-not-display-name-of-dropdown-menu-in-vba/0126575d-afa8-457e-aae7-693789f0ba2a ' Dim ccMyCC As ContentControl Dim objLe As ContentControlListEntry Dim strText As String Dim strValue As String Set ccMyCC = ActiveDocument.ContentControls(1) Let strText = ccMyCC.range.Text For Each objLe In ccMyCC.DropdownListEntries If objLe.Text = strText Then Let strValue = objLe.Value Next MsgBox "The value associated with " & strText & " is " & strValue Set objLe = Nothing Set ccMyCC = Nothing End Sub |
#6
|
|||
|
|||
Lets say the CCs are Titled/Tagged "Condition" and "Value." The "Condition" CC is a dropdown with the display entries "meets" and "exceeds" with the values 0.25 and 0.75. The "Value" CC is a plain text CC.
If you mapped the to CCs to a CustomXMLPart to nodes named "Condition" and "Value" then you could use: Code:
Private Sub Document_ContentControlBeforeStoreUpdate(ByVal ContentControl As ContentControl, Content As String) Select Case ContentControl.Tag Case "Condition" ContentControl.XMLMapping.CustomXMLPart.SelectSingleNode("/ns0:CC_Map_Root[1]/ns0:Value[1]").Text = Content End Select End Sub |
#7
|
|||
|
|||
And, based on Paul's response to another question, here is a function:
Code:
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 |
#8
|
|||
|
|||
Charles,
Again, if the CCs are mapped it is a simple matter of: Code:
Private Sub Document_ContentControlBeforeStoreUpdate(ByVal ContentControl As ContentControl, Content As String) Select Case ContentControl.Type Case 3, 4: Msgbox Content End Select End Sub |
#9
|
|||
|
|||
Quote:
I agree and your method is elegant. Unfortunately most are not mapped and many of the people on these forums seem to be unwilling to try, even with the tools you provide. I map about a third of mine using your tools. |
#10
|
|||
|
|||
Charles,
I agree that most people don't map. Then again, most people using CCs don't need the "Value" of the selected item either. Most people don't even know the difference between the display text and the value (i.e., the display is what is shown in the document, the value is what is stored in the CustomXMLPart (when mapped). Certainly not trying to knock the function you provided. Just pointing out the benefits of mapping CCs. |
Tags |
content control, value not display name |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Code to Sum Column of Content Control Values In Specific Tables? | warbird | Word VBA | 2 | 07-13-2015 05:44 AM |
Word drop-down content control dependent display issue | vvcat | Word | 6 | 02-03-2015 11:20 PM |
how to make building block content control determine bb to display elsewhere | jamles12 | Word VBA | 5 | 11-16-2013 11:38 AM |
Content control merge values? | skrallemand | Word VBA | 8 | 10-02-2013 06:54 AM |
Assigning Values to content control checkboxes and calculating results | creative cathy | Word Tables | 13 | 10-07-2012 08:52 PM |