向内容控制箱添加字符串/用内容控制替换下拉菜单

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

我有一个有效的代码,但是我想用Content Control替换我的Drop-Down,因为我还需要能够手动键入一个值。里面的值是来自https的列表,此字符串可以正常工作,因此请忽略。

这是我的代码:

Dim MyRequest As Object
Dim Data() As String
Dim i As Integer
Dim j As Integer
Dim maxi As Integer

    Set MyRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
    MyRequest.Open "GET", _
    "https... (This is hidden for security resons, /csv/)"

    ' Send Request.
    MyRequest.Send

    'And we get this response
    'MsgBox MyRequest.ResponseText
    Data = Split(MyRequest.ResponseText, "|")

    If UBound(Data()) > 25 Then
        maxi = 25
    Else
        maxi = UBound(Data())
    End If


    For j = 1 To 6
        ActiveDocument.FormFields("Dropdown" & j).DropDown.ListEntries.Clear
        For i = 0 To maxi - 1
            ActiveDocument.FormFields("Dropdown" & j).DropDown.ListEntries.Add Name:=Data(i)
        Next i
    Next j
End Sub
excel vba ms-word contentcontrol
1个回答
0
投票

您不应在同一文档中使用内容控件和表单域。它们并非设计用于这种方式,这样做是已知的问题根源。

如您所见,下拉表单域不支持文本输入。要提供该功能,您可以在下拉菜单中提供“自由文本”选项,并使用带有输入框的退出宏宏将用户的“自由文本”插入到下拉表单字段中。例如,假设您的表单域“ Dropdown1”包含5个项目,最后一个提供自由文本输入。将以下退出宏添加到formfield将提供:

Sub FreeText()
Dim StrNew As String
With ActiveDocument.FormFields("Dropdown1").DropDown
  If .Value = 5 Then
  StrNew = Trim(InputBox("Input your text", "Free text input"))
  If StrNew = vbNullString Then Exit Sub
    .ListEntries(5).Delete
    .ListEntries.Add StrNew
    .Value = 5
  End If
End With
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.