vba 我无法从 ContentControl 获取选定的值,也无法设置我想要的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24349111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I'm not able to get the selected value from ContentControl and I'm not able also to set the value I want
提问by user978144
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.
我无法获得选定的值“男性或女性”,也无法设置我想要的值。
回答by KekuSemau
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:
前段时间查了一下,微软只是忘了让你查询value
一个DropDown-ContentControl的selected 。
您只能获取ContentControl.Range.Text
,因此如果您需要查找相应的速记值,则必须循环遍历:
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.
对于更改,您可以简单地设置 ContentControl 的 .Range.Text
. 它必须匹配现有的 dropdown-listentries-text(区分大小写),以便value
之后返回正确的内容。
回答by user978144
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.
尽管这看起来像是“额外的工作”,但如果您能够将内容控件映射到自定义 XML 部件,则可以直接从映射中获取值。
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
回答by Wade Hatler
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.
答案#1 似乎是正确的。这是为您提供 .Value 的 VSTO C# 版本。您可以使用 .Index 获得序数位置。对于没有选择或不匹配的选择,返回 null。
var currentChoice = cc.DropdownListEntries.Cast<ContentControlListEntry>()
.FirstOrDefault(cl => cl.Text == cc.Range.Text)
?.Value;