1

I have extensively searched and not found what i am looking for. My knowledge of VBA has so far been confined to manipulating numbers so this is a new area for me.

I am attempting to pull the single line of data from the following url LINK. Once that is done, I can work on parsing that data but we'll cross that bridge...

Here is the code so far in Excel VBA:

Enum READYSTATE
    READYSTATE_UNINITIALIZED = 0
    READYSTATE_LOADING = 1
    READYSTATE_LOADED = 2
    READYSTATE_INTERACTIVE = 3
    READYSTATE_COMPLETE = 4
End Enum

Sub ImportStackOverflowData()

    Dim ie As InternetExplorer

    Dim html As HTMLDocument

    Set ie = New InternetExplorer

    ie.Visible = False
    ie.navigate "http://api.rsbuddy.com/grandExchange?a=guidePrice&i=1637"

    Do While ie.READYSTATE <> READYSTATE_COMPLETE
        DoEvents
    Loop

    'show text of HTML document returned
    Set html = ie.document.body
    MsgBox html.DocumentElement.innerHTML

    'close down IE and reset status bar
    Set ie = Nothing
    Application.StatusBar = ""

End Sub

After navigating to the url, the IE downloads window opens, and asks me to Open or Save a .json file... clicking anything crashes the code and ignoring it leaves the code in the READYSTATE loop below it.

How can I stop it from opening this window, and instead insert the info into the workbook?

Thank you.

Community
  • 1
  • 1
MIL-SPEC
  • 123
  • 1
  • 6
  • From my understanding (which is probably wrong) the content of the site is being delivered as a download because of the headers being sent from the server. This is something you have no control over. If you manually navigate to the site do you still get the download prompt? Just a side-note, seeing a you apparently have a reference set to the IE Object Library, you don't need to create the `ReadyState` enum at the top of your module. – SierraOscar Apr 26 '16 at 10:05
  • Thanks, It was part of a guide I was following. will remove it. Also, Is this problem due to IE being unable to display .json natively? Should I switch to another browser? – MIL-SPEC Apr 26 '16 at 10:08
  • Hi sorry, I commented at the same time as your edit. Opening the URL in chrome or firefox displays a line of text. open in IE and it tries to save the file... – MIL-SPEC Apr 26 '16 at 10:10
  • In that case it's the headers - so there's nothing you can do to your current code to change that. You *can* however use another method such as a winHTTP request – SierraOscar Apr 26 '16 at 10:14
  • thanks macro man i will look into using winHTTP – MIL-SPEC Apr 26 '16 at 10:20

1 Answers1

3

Getting around that box isn't trivial, there are many, many posts about it if you search. It gets especially complex since some versions of IE act in a different way (with the download ribbon rather than a dialog box).

To do it the way you are doing it, you need to use Windows APIs to get the handle of the download window and dismiss it. This gets very complicated very quickly.

There is however a much simpler way; don't use Internet Explorer for simple requests. Use something lighter, I've use MSXML here, but you could swap it with WINHTTP.

Sub ImportStackOverflowData()

    Dim request As Object
    Set request = CreateObject("MSXML2.XMLHTTP")

    With request
        .Open "GET", "http://api.rsbuddy.com/grandExchange?a=guidePrice&i=1637", False
        .send
        MsgBox .responseText
    End With

End Sub
SWa
  • 4,343
  • 23
  • 40
  • Thanks for the response Kyle, it makes a lot of sense. Just tried your method and it hangs on the ".send", and eventually returns a runtime error "-2147012894" Automation error. Am I missing a reference? – MIL-SPEC Apr 26 '16 at 10:17
  • @User9123, no, WinHTTP can need some fiddling with depending on your particular network config. Try the amended code – SWa Apr 26 '16 at 10:22
  • Awesome! Thank you. Now to parse the data... :) – MIL-SPEC Apr 26 '16 at 10:26
  • Note that you will probably need to use `MSXML2.ServerXMLHTTP.6.0` nowadays to avoid authorization errors. See https://stackoverflow.com/a/36292949/4178262 – Mark Stewart Jun 17 '22 at 15:56