2

i want to read the HTML code in VBA (something like URL in Java). I need to save it in a string. I parse it afterwards.

alpan67

Alpan67
  • 105
  • 2
  • 5
  • 15
  • Do you mean `URL decoding into a String`? e.g. `java.net.URLDecoder.decode(url, "UTF-8")`; – bonCodigo Dec 31 '12 at 15:20
  • I need to read the HTML code, it is a football table and i decode it later. How can i save the HTML of the website ? In Java i can create a URL and then openconnection etc. etc. – Alpan67 Dec 31 '12 at 15:23

2 Answers2

10

Here's a function for you. It will return the String of the HTML returned by a given URL.

Function GetHTML(URL As String) As String
    Dim HTML As String
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", URL, False
        .Send
        GetHTML = .ResponseText
    End With
End Function

Just make sure your provided URL is well formed. I.E. it includes the http:// or https:// if appropriate.

For Example: GetHtml("www.google.com") is incorrect.
You would want GetHtml("https://www.google.com/")

Daniel
  • 12,982
  • 3
  • 36
  • 60
  • Thanks - how do you avoid the response text being truncated? – Davide Apr 11 '18 at 15:55
  • I used your code and I get "Access denied" error at `.Send`. Any idea why? (Note: I used very common URLs (http://www.google.com, http://www.youtube.com, etc. that normally have no problem to be opened programmatically.) – Apostolos Aug 30 '18 at 17:24
  • Thanks. I found a solution using CreateObject("MSXML2.ServerXMLHTTP.6.0") – Apostolos Aug 30 '18 at 18:55
0
Community
  • 1
  • 1
bonCodigo
  • 14,268
  • 1
  • 48
  • 91