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

发布于 2024-10-12 20:50:20 字数 897 浏览 3 评论 0原文

我正在使用以下代码填充 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 菜鸟的任何帮助表示赞赏

I'm using the following code to populate a combobox in Word 2003.

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

What I would like to do is get the data dynamically via an http call. This other function seems to work to get data via 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 just contains

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

I was hoping to combine the two but below doesn't work

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

Any help for a VBA noob appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍白女子 2024-10-19 20:50:20

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

红、绿、黄、蓝

因此,您可以使用以下方法来填充组合框:

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)

The response text should be a simple comma separated string, something like

Red,Green,Yellow,Blue

Thus you can use the following method for populating the 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

For calling the method you can use the following sentence.

Call populateComboBox(Combo1, MyRequest.ResponseText)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文