Ich verwende this link als Referenz zum Herunterladen einer ZIP-Datei von URL.Verwenden Sie VBA zum Herunterladen von Zip-Datei von URL
Der Code ich verwende ist in unter
Sub DownloadZipExtractCsvAndLoad()
Dim UrlFile As String, ZipFile As String, CsvFile As String, Folder As String, s As String
' UrlFile to the ZIP archive
UrlFile = "https://loanperformancedata.fanniemae.com/lppub/publish?file=2008Q1.zip"
' Extract ZipFile from UrlFile
ZipFile = "2008Q1.zip"
' Define temporary folder
Folder = "C:\Users\xxxxxx\Desktop\"
' Disable screen updating to avoid blinking
Application.ScreenUpdating = False
' Trap errors
On Error GoTo exit_
' Download UrlFile to ZipFile in Folder
If Not Url2File(UrlFile, Folder & ZipFile, "xxx", "xxxx") Then
MsgBox "Can't download file" & vbLf & UrlFile, vbCritical, "Error"
Exit Sub
End If
exit_:
' Restore screen updating
Application.ScreenUpdating = True
' Inform about the reason of the trapped error
If Err Then MsgBox Err.Description, vbCritical, "Error"
End Sub
Function Url2File(UrlFile As String, PathName As String, Optional Login As String, Optional Password As String) As Boolean
'ZVI:2017-01-07 Download UrlFile and save it to PathName.
' Use optional Login and Password if required.
' Returns True on success downloading.
Dim b() As Byte, FN As Integer
On Error GoTo exit_
If Len(Dir(PathName)) Then Kill PathName
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", UrlFile, False, Login, Password
.send
If .Status <> 200 Then Exit Function
b() = .responseBody
FN = FreeFile
Open PathName For Binary Access Write As #FN
Put #FN, , b()
exit_:
If FN Then Close #FN
Url2File = .Status = 200
End With
End Function
jedoch nur jedes Mal, wenn ich den Code ausführen, wird es eine leere Zip-Datei statt dem Herunterladen der Datei erstellen.
Irgendwelche Hilfe?
Wenn Sie zu der tatsächlichen URL gehen, die Sie abrufen möchten (z. B. einfügen ** https: //loanperformancedata.fanniemae.com/lppub/publish? file = 2008Q1.zip ** in die Suchleiste Ihres Browsers), Sie werden sehen, dass die Datei nicht existiert. – ainwood
@ainwood Ich bin neu in diesem Bereich. Diese Website benötigt Login-Informationen. Nachdem ich mich mit meinem Benutzernamen und meinem Passwort angemeldet habe, funktioniert der Link. – kzhang12