VBA Word:在表格的特定单元格内创建下拉菜单 ContentControl 并填充下拉菜单

问题描述 投票:0回答:1

据我所知,在 For 循环中,这将在预期表的底部添加一行。
然后找到最底行的第二个单元格。
然后添加一个下拉列表并将其命名为“主要角色”。
然后将所有属性设置为下拉列表的内容。
稍后,它将循环返回此处,获取所需行数所需的次数。
这样,随着表变长,每一行都有相同下拉列表的副本。

'PersonnelTbl.Rows.Add
Set RoleCell = PersonnelTbl.Cell(PersonnelTbl.Rows.Count, 2).Range
With RoleCell.ContentControls.Add(wdContentControlDropdownList)
    .Title = "Primary Role"
    .SetPlaceHolderText , , "Role"
    .DropdownListEntries.Add "A"
    .DropdownListEntries.Add "B"
    .DropdownListEntries.Add "C"
    .DropdownListEntries.Add "D"
End With

问题是这些被视为“类型不匹配”:

.SetPlaceHolderText , , "Role"
.DropdownListEntries.Add "A"
.DropdownListEntries.Add "B"
.DropdownListEntries.Add "C"
.DropdownListEntries.Add "D"

如果我将其扩展至其完整的正确值:

RoleCell.ContentControls.Add(wdContentControlDropdownList).SetPlaceHolderText , , "Role"

它返回为“无效命令”

我是否“复制”下拉列表并将其粘贴到新创建的单元格中?我是否只是为用例指定了错误的下拉值?

vba ms-word word-2016
1个回答
0
投票

我在您的 With RoleCell 行上收到“需要对象”错误。这是工作内容控制代码的示例:

Sub AddDropDownCC()
  Dim oCC As ContentControl
  Set oCC = ActiveDocument.ContentControls.Add(wdContentControlDropdownList, Selection.Range)
  With oCC
    .SetPlaceholderText Text:="Role"
    .DropdownListEntries.Add "Choose an item.", value:=""
    .DropdownListEntries.Add "Item 1", "Value 1"
    .DropdownListEntries.Add "Item 2", "Value 2"
  End With
  Set oCC = Nothing
End Sub

.DropdownListEntries.Add 需要 2 个字符串,其中 1 个用于显示的文本,另一个用于选择该文本时返回的值。

© www.soinside.com 2019 - 2024. All rights reserved.