我无法从ContentControl获取所选值,我也无法设置我想要的值

时间:2014-06-22 07:37:01

标签: vba word-vba

我正在尝试创建一个从属下拉列表,用户可以在其中选择第一个下拉列表,所有其他相关下拉列表将自动更改。

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

我无法获得所选的“男性或女性”值,而且我也无法设置我想要的值。

3 个答案:

答案 0 :(得分:3)

从我上次查看的内容来看,Microsoft只是忘了让您查询DropDown-ContentControl的选定value。 你只能获得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

要进行更改,您只需设置ContentControl' .Range.Text即可。它必须匹配现有的dropdown-listentries-text(区分大小写)才能在之后返回正确的value

答案 1 :(得分:0)

虽然看起来似乎是"额外的工作",如果您能够将内容控件映射到自定义XML部件,您可以直接从映射中获取值。

作为一个例子(你必须更加努力才能正确地做到这一点),从新文档开始:

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

然后您可以使用(例如)

来检索值
activedocument.ContentControls(1).XMLMapping.CustomXMLNode.Text

答案 2 :(得分:0)

答案#1似乎是正确的。这是VSTO C#版本,它为您提供.Value。您可以使用.Index获取序数位置。对于无选择或不匹配的选择,返回null。

let stuff = firstly {
    try DoStuff.doStuff()
}.then { response in
    self.array = response
}

let otherStuff = firstly {
    try DoOtherThing.otherThing()
}.then { response in
    self.stuff = response
}

when(fulfilled: stuff, otherStuff).catch { _ in
    let alertController = UIAlertController(title: "Network Error", message: "Network error, please try again", preferredStyle: .alert)
    let OKAction = UIAlertAction(title: "OK", style: .default) { (action) in
        //
    }
    alertController.addAction(OKAction)
    self.present(alertController, animated: true) {
        //
    }
}