【问题标题】:I'm not able to get the selected value from ContentControl and I'm not able also to set the value I want我无法从 ContentControl 中获取选定的值,也无法设置我想要的值
【发布时间】:2014-08-12 12:01:14
【问题描述】:

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

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

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

【问题讨论】:

    标签: vba ms-word


    【解决方案1】:

    根据我前段时间的查找,Microsoft 只是忘记让您查询 DropDown-ContentControl 的选定value
    只能得到ContentControl.Range.Text,所以如果需要查找对应的shorthand-value,就得循环遍历:

    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。它必须匹配现有的下拉列表文本(区分大小写),以便之后返回正确的value

    【讨论】:

      【解决方案2】:

      虽然这看起来像是“额外的工作”,但如果您能够将内容控件映射到自定义 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
      

      【讨论】:

        【解决方案3】:

        答案#1 似乎是正确的。这是为您提供 .Value 的 VSTO C# 版本。您可以使用 .Index 获得序数位置。如果没有选择或选择不匹配,则返回 null。

                var currentChoice = cc.DropdownListEntries.Cast<ContentControlListEntry>()
                                      .FirstOrDefault(cl => cl.Text == cc.Range.Text)
                                      ?.Value;
        

        【讨论】:

          猜你喜欢
          • 2020-04-03
          • 1970-01-01
          • 2016-08-26
          • 1970-01-01
          • 2011-03-27
          • 2021-10-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多