Copy URL file to local folder

JohnPapa

Registered User.
Local time
Today, 11:09
Joined
Aug 15, 2010
Messages
1,020
The following used to work. Now it cannot find the file and WinHttpReq.status = 404.


Code:
On Error GoTo Failed
 
 Dim oStream As Variant

 Dim WinHttpReq As Object
 Set WinHttpReq = CreateObject("MSXML2.ServerXMLHTTP.6.0")

 WinHttpReq.Open "GET", FileURL, False, Username, Password
 WinHttpReq.send
If WinHttpReq.status = 200 Then
    Set oStream = CreateObject("ADODB.Stream")
    oStream.Open
    oStream.Type = 1
    oStream.Write WinHttpReq.responseBody
    Dim OWrite As Integer
    If DontOverWrite = True Then
     OWrite = 1
    Else
     OWrite = 2
    End If
    oStream.SaveToFile LocalFolderFile, OWrite
    oStream.Close

    Exit Sub
End If
Failed:
 Debug.Print "Failed to DL " & URLStr
 
So, your FileURL returns a 404.

Did you double-check the url?

What are the values of Username and Password when you make the request?

Are they correct? Did you double-check?

What happens if you try it manually in a browser? (You can use http://user:pw@url)
 
The following used to work. Now it cannot find the file and WinHttpReq.status = 404.


Code:
On Error GoTo Failed
 
 Dim oStream As Variant

 Dim WinHttpReq As Object
 Set WinHttpReq = CreateObject("MSXML2.ServerXMLHTTP.6.0")

 WinHttpReq.Open "GET", FileURL, False, Username, Password
 WinHttpReq.send
If WinHttpReq.status = 200 Then
    Set oStream = CreateObject("ADODB.Stream")
    oStream.Open
    oStream.Type = 1
    oStream.Write WinHttpReq.responseBody
    Dim OWrite As Integer
    If DontOverWrite = True Then
     OWrite = 1
    Else
     OWrite = 2
    End If
    oStream.SaveToFile LocalFolderFile, OWrite
    oStream.Close

    Exit Sub
End If
Failed:
 Debug.Print "Failed to DL " & URLStr
EDIT: The file to be downloaded is a .zip file. I execute the same code before and successfully download a .txt file
 
So, your FileURL returns a 404.

Did you double-check the url?

What are the values of Username and Password when you make the request?

Are they correct? Did you double-check?

What happens if you try it manually in a browser? (You can use http://user:pw@url)
WinHttpReq.status returns 404

When I type the url, it shows me the file to be downloaded

Username and Password are the correct values

As I mention in the EDIT above

The file to be downloaded is a .zip file. I execute the same code before and successfully download a .txt file
 
1. Do you have Option Explicit declared at the top of all your code modules?

2. Show the calling code of the incomplete code you posted above.

When I type the url, it shows me the file to be downloaded
This suggests that the url is not actually a link to the file you want to download, but a page showing the link to the file you want to download.
 
Last edited:
1. Do you have Option Explicit declared at the top of all your code modules?

2. Show the calling code of the incomplete code you posted above.


This suggests that the url is not actually a link to the file you want to download, but a page showing the link to the file you want to download.
Problem solved. It was a matter of case. The code was trying to download a file called "vd32.zip" and the actual file was "VD32.zip"

Thanks for all the help.
 

Users who are viewing this thread

Back
Top Bottom