我正在使用以下代码在 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/