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.

0

2 Answers 2

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.Send
httpRequest.WaitForResponse
Debug.Print httpRequest.Status
Debug.Print httpRequest.ResponseText
mystring = httpRequest.ResponseText

Then you get the file in the var name mystring

Sign up to request clarification or add additional context in comments.

1 Comment

This works great, except it seems you forgot the: codehttpRequest.Sendcode Before the WaitForResponse
0
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.send
httpRequest.WaitForResponse
Debug.Print httpRequest.Status
Debug.Print httpRequest.ResponseText
mystring = httpRequest.ResponseText

Have to add httpRequest.send before httpRequest.WaitForResponse

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.