VBA eating memory.. cannot clear resources - vba

I'm working on a VBA code using xmlHTTP, and I have a problem that I couldn't figure out its reason..
The problem is:
while the loop is running it keeps consuming an increasing amount of memory, so I tried to find the part that causes this issue, and I came up with this part:
For i = 1 To 100
url = "https://www.google.co.in/search?q=" & "hello" & "&rnd=" & WorksheetFunction.RandBetween(1, 10000)
Set XMLHTTP = CreateObject("MSXML2.serverXMLHTTP")
XMLHTTP.Open "GET", url, False
XMLHTTP.setRequestHeader "Content-Type", "text/xml"
XMLHTTP.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:25.0) Gecko/20100101 Firefox/25.0"
XMLHTTP.send
Set html = CreateObject("htmlfile")
html.body.innerHTML = XMLHTTP.responsetext
DoEvents
Next i
I tried everything I can think of (tried to set objects to nothing -inside & outside the loop-, and tried to save the excel file from inside the 'for loop') but I couldn't solve the problem.
I tried the following 3 lines and they are NOT fixing anything:
XMLHTTP.abort
Set XMLHTTP = Nothing
Set html = Nothing
Note #1: the problem occurs even when these 11 lines of code are the only code to be executed!
Note #2: The memory won't be freed even after the procedure is completed!
Attached a screenshot of memory usage while the program is running:
Please advise.. thank you

The fix for the issue that Benoit refers to above started rolling out to Office 2016 customers earlier this week. If you don't have the update yet, go to File > Account > Update Options > Update Now.
Anneliese from the Office team

Related

USGA - GHIN API - Invalid Token

There are a couple of similar questions here, but the answers don't seem to go quite far enough to solve my issue. I'm using the code provided by "asmitu" (thank you!) but it's getting a response of "Error:Invalid Token".
Const Url = "https://api2.ghin.com/api/v1/public/login.json?"
Const Link = "https://api2.ghin.com/api/v1/followed_golfers/"
Dim Http As New XMLHTTP60, ghinNum$, lastName$
ghinNum = "valid-ghin-number"
lastName = "valid-password"
With Http
.Open "GET", Url & "ghinNumber=" & ghinNum & "&lastName=" & lastName & "&remember_me=false", False
.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
.setRequestHeader "Referer", "https://www.ghin.com/login"
.send
.Open "GET", Link & ghinNum & ".json", False
.send
End With
MsgBox Http.responseText
This site has some info on the API -
https://app.swaggerhub.com/apis-docs/GHIN/Admin/1.0#/User%20APIs/post_users_login__format_
But I can't figure how to capture a valid token from the first API call above to use to pass to the API for the second call to the API under the same GHIN number and password. If someone has gotten this to work, can you help a brother out, please? Thank you.
I reached out to the USGA GHIN folk and, surprisingly and thankfully, got an answer. Not the answer I wanted but there you go. Basically, they said: "they have a requirement that your software supports 10 clubs or 1,400 golfers." So that lets me out.

Http request VBA response

Making my first steps on HTTP within VBA.
Already managed to get cookie value in order to make a request. The problem I'm having is that the file I want to download is not in the first response. When I analyse using HTTP Header Live, the browser receives several responses and only the last one is the file, a PDF that is generated after a query sent by the user. The only thing I'm getting is the first response that I'm displaying with a MsgBox. Can someone help me solving this problem. Made some searches through the web but haven't found yet, a solution.
The code I am using is:
Sub Test()
Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("WinHTTP.WinHTTPrequest.5.1")
With WinHttpReq
.Open "POST", myURL, False ', "username", "password"
.send
x = .getResponseHeader("Set-Cookie")
i = InStr(x, ";")
x = Left(x, i - 1)
MsgBox x
End With
With WinHttpReq
.Open "GET", myURL, False
.Option(WinHttpRequestOption_EnableRedirects) = True
.SetRequestHeader "Content-Type", "application/pdf"
.SetRequestHeader "Accept-Encoding", "gzip, deflate, br"
.SetRequestHeader "Cookie", x
.SetRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0"
.send
End With
MsgBox (WinHttpReq.getAllResponseHeaders())
If WinHttpReq.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
With oStream
.Type = 1
.Open
.Write WinHttpReq.responseBody
.SaveToFile "C:\Users\xxx\Desktop\file.pdf", 2
.Close
End With
End If
Set WinHttpReq = Nothing
Set oStream = Nothing
End Sub
Analyzing with Firefox, when I enter the URL, I receive two responses with 200 OK. I wonder how can I get the second response? The site uses a javascript that interprets the query I send and returns a PDF file. The file name changes according to the user and the query.
Now, i reached the following point. I make a first request to get the cookie, then a second to get an ETag from a diferent address. The problem is that when i make the third request to download the file, the filename is apparently generated by the server (APACHE?), based on the ETag and something I'm not being able to find. The last 5 numbers from the ETag change in the filename.
For example:
ETag - 1543932096000
File - 1543932095115.xxxxx.address.com.5245.idp.pdf
Since I don't have the filename, i cannot download the file with an httprequest.
Help?

Excel Macro http GET won't connect to internet

I have written a short macro [below] to test a GET call. This macro works fine for several people that have tested it, however one person (using same version of excel - 2013) is getting a timeout error. I have allowed Excel to connect to the internet in settings, and have enabled macros. Any suggestions are appreciated.
Thanks!
Set objHTTP = CreateObject("Msxml2.ServerXMLHTTP.6.0")
Dim result As String
URL = "http://www.google.com"
With objHTTP
.Open "GET", URL, False
.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
.send ("")
End With
Change object type: Set objHTTP = CreateObject("Microsoft.XMLHTTP")
Hope this help!

HTTP request freezes when I run the program, but not when I step in

the program runs from an excel workbook,takes a list of values, builds a query string and passes it to this chunk of code
Set xmlHttp = CreateObject("MSXML2.XMLHTTP.6.0")
xmlHttp.Open "GET", connectionstring, False
xmlHttp.setRequestHeader "Content-Type", "text/xml"
xmlHttp.send
Dim html As Object
Set html = CreateObject("htmlfile")
html.body.innerHTML = xmlHttp.ResponseText
This is part of a loop, so it gets executed many times.
If I step into the code and execute it line by line there is no problem, but when I run it it freezes on the xmlHttp.send line, not at the first loop, but somewhere in the middle of the loop (around the 10th execution maybe).
Do I have to tell it to wait for soething in particular?
Any Ideas?
Thanks
XMLHTTP has a ReadyState property like IE :
So this is how to use it :
xmlHttp.send
Do While xmlHttp.readyState <> 4
DoEvents
Loop

REST Interacting with BigCommerce from VB EXCEL

NOTE: Further research (and thick skin, and lots of coffee) has lead me here where it suggests that the store cannot receive the uid and pwd in the URL, and I gather from the PHP examples here that the transmission needs to be encrypted. Is this my problem? If so I have not been able to find anything on how to work around this.
I would be happy if the open() command prompted the UID & PWD window to open and I can manually input the values. Does anyone have a suggestion. Thanks!
I am learning how to use the BigCommerce APIs and I am programming in VB / XL to be able to directly post / retrieve from all fields in the store's DB.
I have never coded on this before (although I get around VB OK) and I am stuck. I have the following code:
Const URL As String = "https://www.myurl.com/api/v2/brands.json"
Public Sub Test()
Dim xmlHttp As Object
Set xmlHttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")
xmlHttp.Open "GET", URL, False, "myid", "mytoken"
xmlHttp.setRequestHeader "Content-Type", "application/json"
xmlHttp.send
Dim html As MSHTML.HTMLDocument
Set html = New MSHTML.HTMLDocument
html.body.innerHTML = xmlHttp.ResponseText
Range("A1").Value = html.body.innerHTML
End Sub
What I get back is "401" or more precisely:
[{"status":401,"message":"No credentials were supplied in the request."}]
The credentials are valid and working, as if I place the URL in my browser and submit it, the pop up for UID & PWD comes up, and once I place the values I am using in the code, the resulting BRANDS list appears in the browser.
Additionally, if I add a "rand num" to the URL to ensure the returned data is not cached as suggested here, then the request looks like this:
xmlHttp.Open "GET", URL & "&t=" & WorksheetFunction.RandBetween(1, 99), False, `"myid", "mytoken"`
and the response back from BC changes to:
406</STATUS><MESSAGE>The requested content type is not available.</MESSAGE></ERROR></ERRORS>
I have also tried this (where UID and PWD are the correct values):
xmlHttp.Open "GET", URL & "&user=UID" & "&password=PWD", False
and i get:
406</STATUS><MESSAGE>The requested content type is not available.</MESSAGE></ERROR></ERRORS>
Can anyone help me understand pls. I have followed examples at MSDN, but I am still stuck. The BC API site is poor in examples (none actually!) for VB.
Thank you
Sorted! BC required this:
Set xmlHttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")
xmlHttp.Open "GET", URL_Cat, UID, PWD
xmlHttp.setRequestHeader "Content-Type", "application/json"
xmlHttp.setRequestHeader "Authorization", "Basic " & Base64Encode(UID & ":" & PWD)
xmlHttp.send
I got Base64Encode code from here. Hope this helps someone.