2011-01-19 73 views
2

我用下面的代碼填充ComboBox在Word 2003中微軟Word VBA,試圖從一個HTTP調用用數據填充組合框

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小老鴨讚賞

回答

1

響應文本應該是一個簡單的逗號分隔字符串,像

紅,綠,黃,藍

因此,你可以用下面的方法來填充組合框:

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) 
+0

工作過一種享受Arce,謝謝 – Saul 2011-01-22 20:19:20