Using/Refresh 关系下拉菜单

Using/Refresh relational dropdowns

我正在尝试使用 VBA-代码设置一个下拉菜单,该下拉菜单依赖于 Word 中先前下拉菜单的 selection。我看了一堆视频并浏览了论坛,但我无法让它发挥作用。我使用了 Word Legacy 下拉菜单并正确标记了它们,然后我在 VBA:

中编写了以下代码
    Dim xDirection As FormField
    Dim xState As FormField
    On Error Resume Next
    Set xDirection = ActiveDocument.FormFields("ddType")
    Set xState = ActiveDocument.FormFields("ddSelection")
    If ((xDirection Is Nothing) Or (xState Is Nothing)) Then Exit Sub
    With xState.DropDown.ListEntries
    .Clear
        Select Case xDirection.Result
            Case "Numbers"
                .Add "1"
                .Add "2"
                .Add "3"
                .Add "4"
                .Add "5"
                .Add "6"
            Case "Letters"
                .Add "A"
                .Add "B"
                .Add "C"
            Case "None"
                .Add "Not applicable"
    End Select
    End With
End Sub

问题是这个解决方案只是有时有效,而不是始终如一。感觉可能的 select 离子更新得不够快,即使只有数字可用,我也可以选择一个字母(有时我根本无法 select 任何东西)。

我在 Office 365 中执行此操作。

非常感谢您的反馈

提前致谢!

假设:您有一个带有两个下拉内容控件的 word 文档。 两者都设置了标签名称:ccType 和 ccSelection。

ThisDocument 的 class 模块中输入以下代码:

Option Explicit

Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
If ContentControl.Tag = "ccType" Then
    fillccSelection ContentControl.Range.Text
End If
End Sub

Private Sub fillccSelection(valueType As String)

Dim cc As ContentControl
Set cc = ThisDocument.SelectContentControlsByTag("ccSelection")(1)

If cc.Title <> valueType Then
    With cc
        .Title = valueType  'set title to current type so that we now if coming here next time
        .Range.Text = vbNullString  'clear content as it has to change with new values
        With .DropdownListEntries
            .Clear
            Select Case valueType
                Case "Numbers"
                    cc.SetPlaceholderText Text:="Please select a number"
                    .Add "1"
                    .Add "2"
                    .Add "3"

                Case "Letters"
                    cc.SetPlaceholderText Text:="Please select a letter"
                    .Add "A"
                    .Add "B"
                    .Add "C"
            End Select
        End With
    End With
End If
End Sub

每当您更改第一个内容控件 (ccType) 的值并 exit 时,ContentControlOnExit 就会被触发。

如果您“离开”了 ccType(而不是 ccSelection),则通过传递在 ccType 中选择的值来调用 fillccSelection。

如果尚未为 ccSelection 设置此类型,则会根据所选类型设置下拉条目。