getHTTP with (excel) VBA?

前端 未结 2 2007
有刺的猬
有刺的猬 2020-12-15 10:48

I\'m searching for a query for an excel VBA macro to get a webpage html code as string. I\'ve found some source with getHTTPrequest but I can\'t register .net framwork syste

相关标签:
2条回答
  • 2020-12-15 10:51

    Close enough: How can I send an HTTP POST request to a server from Excel using VBA? — It's even for Excel ;-)

    Just use a GET request instead:

    objHTTP.Open "GET", URL, False
    

    MSDN: Using the WinHttpRequest COM Object - Retrieving Data Using Visual Basic

    0 讨论(0)
  • 2020-12-15 11:10

    Here's a compact function that returns the response text of almost any specified URL, such as HTTP source code for a web page, or response text from JSON API's.


    Late Bound:

    (No references required)

    Public Function getHTTP(ByVal url As String) As String
      With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", url, False: .Send
        getHTTP = StrConv(.responseBody, vbUnicode)
      End With
    End Function
    

    Early Bound:

    If you need to call the function repeatedly (in a loop, for example) you might be better off to use it as an early-bound function:

    1. Add a reference to the XML library:

      ToolsReferences → Select

    0 讨论(0)
提交回复
热议问题