vba - MS Word VBA,试图用来自 http 调用的数据填充组合框

标签 vba http combobox ms-word

我正在使用以下代码在 Word 2003 中填充组合框。

Private Sub UserForm_Initialize()
ComboBox1.List = Array("Red", "Green", "Yellow", "Blue")
End Sub

我想做的是通过 http 调用动态获取数据。这个其他功能似乎可以通过 http 获取数据

Dim MyRequest As Object

Set MyRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
MyRequest.Open "GET", _
"http://localhost:8500/test7.htm"

' Send Request.
MyRequest.Send

'And we get this response
MsgBox MyRequest.ResponseText

test7.htm 只包含

"Red", "Green", "Yellow", "Blue"

我希望将两者结合起来,但下面不起作用

Private Sub UserForm_Initialize()
Dim MyRequest As Object

Set MyRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
MyRequest.Open "GET", _
"http://localhost:8500/test7.htm"

' Send Request.
MyRequest.Send


ComboBox1.List = Array(MyRequest.ResponseText)
End Sub

感谢对 VBA 新手的任何帮助

最佳答案

响应文本应该是简单的逗号分隔字符串,类似于

Red,Green,Yellow,Blue

因此您可以使用以下方法来填充 ComboBox:

Private Sub populateComboBox(ByRef combo As ComboBox, ByRef html As String)
    Dim arrayValues() As String, index As Integer
    arrayValues = Split(Trim(html), ",")
    For index = LBound(arrayValues) To UBound(arrayValues)
        combo.AddItem (arrayValues(index))
    Next
End Sub

要调用该方法,您可以使用以下句子。

Call populateComboBox(Combo1, MyRequest.ResponseText)

关于vba - MS Word VBA,试图用来自 http 调用的数据填充组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4731762/

相关文章:

vba - 查找部分字符串以从相应的列和行返回数据 (VBA)

vba - 如何从脚本字典将数组添加到单元格内下拉列表中?

ms-access - 是否可以在 MS Access MsgBox 中放置组合框(下拉列表)?

java - 激活和停用 ComboBox

java - 选择其他 JComboBox 值时清除 JList

vba - Access VBA - 在 Excel Sheet1 列 A 中搜索值并将相应的值放入 B 列?

vba - 使用 Excel 2018 for MacOS 的用户表单

android - ionic 应用程序无法连接到本地主机

python sys.stdout.write() 重定向

java - Android 中的通用网络管理器