Search code examples
excelvbawinhttprequest

Why does WinHTTPRequest responseText return a different value to MsgBox than Debug.Print dynamic web pages


I encountered something rather curious today while preparing to write a pure vba simple javascript engine. Pulling the document from http, I'd expect to get what it outputs in MsgBox. The plaintext code of the website with javascript tags, etc. I can work with that. Only, I can't work with it if it's only outputting the way I need to the msgbox or a wscript object popup. But if you try to print it to a file, to a cell, etc... the result is what you see in debug.print.

The response in messagebox is the full javascript code and html of the website the way it would show up in a browser if you view page source. The response in debug.print is a different html saying "We're glad you want to do more with your quickbase. Trying to scrape a quickbase page without using the quickbase api.

I'm very curious what is going on here. Different variable type? VarType doesn't indicate that. It can't be something happening on the server's end. Not in my mind anyway. Read through the documentation and I just can't figure it out. What's going on?

grab = CreateObject(WinHttp.WinHttpRequest.5.1")
grab.Open
.SetRequestHeaders "irrelevant", "Really doesn't matter what I'm setting as my headers"
grab.send
' Here's the weird part...
resptext = grab.responseText
respbody = grab.responseBody
debug.print(resptext)
MsgBox(resptext)
string_of_byte_array = StrConv(respbody, vbUnicode)
debug.print(string_of_byte_array)
MsgBox(string_of_byte_array)

Appreciate any clarification you can offer.


Solution

  • If you send a long string to the Immediate pane (or a series of shorter strings) using Debug.Print, you're only going to see the last 200 lines or so: that's a built-in limit when using the Immediate pane.
    Msgbox would show the beginning of the content (the max size of msgbox content is approx. 1024 characters). Max. length in a cell is about 32k characters.