0

How to download a file from URL to a string variable without saving it to local drive? Similarly to this method in C# I would like it in VBA: Download file from URL to a string

Here is something with download file to disc space: How do i download a file using VBA (Without internet explorer) While it can be some hint, I would like to avoid this intermediate step of getting the file to string.

Community
  • 1
  • 1
Przemyslaw Remin
  • 6,276
  • 25
  • 113
  • 191

1 Answers1

1

With WinHttpRequest class you can achieve the file, the .Open method need 2 args: the request method "GET"/"POST", the URL string.
The third arg is optional, boolean, default is True for asynchronous mode:

Set httpRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
URL = "http://urlo_of_the_file_you_want"
httpRequest.Open "GET", URL, False
httpRequest.setRequestHeader "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"
httpRequest.WaitForResponse
Debug.Print httpRequest.Status
Debug.Print httpRequest.ResponseText
mystring = httpRequest.ResponseText

Then you get the file in the var name mystring

Trimax
  • 2,413
  • 7
  • 35
  • 59