问题
I'm trying to make a dependent drop-down list where the user may select the first drop-down list and all the other dependent drop-down list will change automatically.
Select Case ContentControl.Title
Case "T1_1"
Select Case ContentControl.DropdownListEntries.Item.Value
Case "male"
ActiveDocument.SelectContentControlsByTitle("T1_2").Item(1).Value = "male"
ActiveDocument.SelectContentControlsByTitle("T1_3").Item(1).Value = "male"
ActiveDocument.SelectContentControlsByTitle("T1_4").Item(1).Value = "male"
Case "female"
ActiveDocument.SelectContentControlsByTitle("T1_2").Item(1).Value = "female"
ActiveDocument.SelectContentControlsByTitle("T1_3").Item(1).Value = "female"
ActiveDocument.SelectContentControlsByTitle("T1_4").Item(1).Value = "female"
End Select
I'm not able to get the selected value "male or female" and I'm not able also to set the value I want.
回答1:
From what I looked up some time ago, Microsoft just forgot to let you query the selected value
of a DropDown-ContentControl.
You can only get ContentControl.Range.Text
, so if you need to look up the corresponding shorthand-value, you have to loop through:
Public Function getCCDD_value(cc As ContentControl) As String
getCCDD_value = ""
For Each Item In cc.DropdownListEntries
If Item.Text = cc.Range.Text Then
getCCDD_value = Item.Value
End If
Next
End Function
For changing, you can simply set the ContentControl's .Range.Text
. It must match an existing dropdown-listentries-text (case sensitive) in order to return the correct value
afterwards.
回答2:
Although it may seem like "extra work", if you are in a position to map your content control to a Custom XML Part, you can get the value directly from the mapping.
As an example (you would have to work somewhat harder to do this correctly), starting with a new document:
Sub insertTestDDLCCandCXP()
Dim cc As Word.ContentControl
Dim l As Long
Dim sCXP As String
For l = ActiveDocument.CustomXMLParts.Count To 4 Step -1
ActiveDocument.CustomXMLParts(l).Delete
Next l
sCXP = "<?xml version='1.0' encoding='utf-8'?><ccData xmlns='bibadia1'><ccDDL1Value/></ccData>"
With ActiveDocument
' add a part
.CustomXMLParts.Add sCXP
' clear out the document
.Range.Delete
Set cc = .ContentControls.Add(wdContentControlDropdownList)
With cc
.DropdownListEntries.Add "dt1", "val1"
.DropdownListEntries.Add "dt2", "val2"
.DropdownListEntries.Add "dt3", "val3"
' using "ns0" is a kludge - you should determine the namespace that
' Word wants to use
.XMLMapping.SetMapping ("//ns0:ccData/ns0:ccDDL1Value")
End With
End With
End Sub
You can then retrieve the Value using (again for example)
activedocument.ContentControls(1).XMLMapping.CustomXMLNode.Text
回答3:
Answer #1 seems to be the right one. Here's the VSTO C# version that gives you the .Value. You can get the ordinal position with .Index. Returns null for no or mismatched selection.
var currentChoice = cc.DropdownListEntries.Cast<ContentControlListEntry>()
.FirstOrDefault(cl => cl.Text == cc.Range.Text)
?.Value;
来源:https://stackoverflow.com/questions/24349111/im-not-able-to-get-the-selected-value-from-contentcontrol-and-im-not-able-also