从 FTP url 下载 VBA 文件 [英] VBA download file from FTP url

查看:24
本文介绍了从 FTP url 下载 VBA 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建 VBA 代码以从直接 FTP 链接(异步首选)将文件下载到特定路径.我只找到了使它与 http url 一起工作的代码,但是对于 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-server 设置用户名或密码.

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:FTPfile.txt", 2 ' 1 = no overwrite, 2 = overwrite
    oStream.Close
End If

End Sub

推荐答案

您需要在项目中添加一个模块才能获得 FTP 功能.子 FTP 下载有示例代码.取自 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:	est.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

这篇关于从 FTP url 下载 VBA 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆