MS Word VBA,尝试使用来自http调用的数据填充组合框

时间:2011-01-19 04:34:16

标签: http combobox word-vba

我正在使用以下代码填充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 noob赞助的帮助

1 个答案:

答案 0 :(得分:1)

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

  

红,绿,黄,蓝

因此,您可以使用以下方法填充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)