VBA从FTP URL下载文件 [英] VBA download file from FTP url
问题描述
我试图创建VBA代码从直接FTP链接(异步首选)将文件下载到特定路径。
我只找到代码,使其与http urls一起使用,但对于FTP,我收到此错误:
Im trying to create VBA code to download a file to specific path from direct FTP link (asynchronously preferred). I only found code for making it work with http urls, but for FTP i get this error:
运行时错误-2146697210(800c0006) ':
系统找不到指定的对象
"Run-time error '-2146697210 (800c0006)': The system cannot locate the object specified"
对于这些第一个测试没有为ftp服务器设置用户名或密码。
For these first testing have not set username or password for the ftp-server.
我的仅适用于http的代码如下:
My code which is working only for http is below:
Sub DownloadFile()
Dim myURL As String
myURL = "ftp://xxx.xxx.xxx.xxx/test.txt"
Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", myURL, False, "username", "password"
WinHttpReq.send
myURL = WinHttpReq.responseBody
If WinHttpReq.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write WinHttpReq.responseBody
oStream.SaveToFile "C:\FTP\file.txt", 2 ' 1 = no overwrite, 2 = overwrite
oStream.Close
End If
End Sub
推荐答案
您将需要向项目添加一个模块以获取FTP功能。 Sub FTPdownload具有示例代码。摘自 http://experts-exchange.com/Networking/Protocols/Q_23627204.html
You will need to add a module to your project to get the FTP functionality. Sub FTPdownload has sample code. Taken from http://experts-exchange.com/Networking/Protocols/Q_23627204.html
Private Const FTP_TRANSFER_TYPE_UNKNOWN As Long = 0
Private Const INTERNET_FLAG_RELOAD As Long = &H80000000
Private Declare Function InternetOpenA Lib "wininet.dll" ( _
ByVal sAgent As String, _
ByVal lAccessType As Long, _
ByVal sProxyName As String, _
ByVal sProxyBypass As String, _
ByVal lFlags As Long) As Long
Private Declare Function InternetConnectA Lib "wininet.dll" ( _
ByVal hInternetSession As Long, _
ByVal sServerName As String, _
ByVal nServerPort As Long, _
ByVal sUsername As String, _
ByVal sPassword As String, _
ByVal lService As Long, _
ByVal lFlags As Long, _
ByVal lcontext As Long) As Long
Private Declare Function FtpGetFileA Lib "wininet.dll" ( _
ByVal hConnect As Long, _
ByVal lpszRemoteFile As String, _
ByVal lpszNewFile As String, _
ByVal fFailIfExists As Long, _
ByVal dwFlagsAndAttributes As Long, _
ByVal dwFlags As Long, _
ByVal dwContext As Long) As Long
Private Declare Function InternetCloseHandle Lib "wininet" ( _
ByVal hInet As Long) As Long
Sub FtpDownload(ByVal strRemoteFile As String, ByVal strLocalFile As String, ByVal strHost As String, ByVal lngPort As Long, ByVal strUser As String, ByVal strPass As String)
'usage
'FtpDownload "/TEST/test.html", "c:\test.html", "ftp.server.com", 21, "user", "password"
Dim hOpen As Long
Dim hConn As Long
hOpen = InternetOpenA("FTPGET", 1, vbNullString, vbNullString, 1)
hConn = InternetConnectA(hOpen, strHost, lngPort, strUser, strPass, 1, 0, 2)
If FtpGetFileA(hConn, strRemoteFile, strLocalFile, 1, 0, FTP_TRANSFER_TYPE_UNKNOWN Or INTERNET_FLAG_RELOAD, 0) Then
Debug.Print "done"
Else
Debug.Print "fail"
End If
InternetCloseHandle hConn
InternetCloseHandle hOpen
End Sub
这篇关于VBA从FTP URL下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!