如何使用VBA for Microsoft Excel使用ftp将数据下载到电子表格

时间:2019-02-08 14:36:19

标签: excel vba ftp

我发现以下代码使我开始开发一种自动方法来从ftp站点下载文件。但是,它每次都失败,没有任何我可以解决的错误。

我在excel vba中有一个新手,所以可以得到一些帮助。

我尝试过在线搜索,但在此处出现堆栈溢出的情况,但我自己无法解决

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, Optional ByVal strUser As String, Optional ByVal strPass As String)
    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 "Success"
Else
    Debug.Print "Fail"
End If

'Close connections
InternetCloseHandle hConn
InternetCloseHandle hOpen

End Sub

Sub Get_File_From_FTP()

'Assign Host URL, Source and Destination File path
Dim HostURL, fileSource, FileDestination As String
HostURL = ThisWorkbook.Sheets(1).Cells(1, 1)
fileSource = ThisWorkbook.Sheets(1).Cells(1, 2)
FileDestination = ThisWorkbook.Sheets(1).Cells(2, 2)
FtpDownload fileSource, FileDestination, HostURL, 21, "Username", "Password"

End Sub

2 个答案:

答案 0 :(得分:0)

好的,以上代码可以正常工作。我相信问题可能出在您的参考单元内...

HostURL = ThisWorkbook.Sheets(1).Cells(1, 1)
fileSource = ThisWorkbook.Sheets(1).Cells(1, 2)
FileDestination = ThisWorkbook.Sheets(1).Cells(2, 2)

当我把它弄乱时,我发现我必须有这种格式...

HostURL = "192.168.168.2" 'Or your FQDN
fileSource = "/This/Is/The/Path/To/Your/File.file" ' Make sure that this matches your file path
FileDestination = "C:\This\Is\The\Complete\Path\On\Your\Machine.File"

当然,请确保您填充了宏的用户名和密码部分...

FtpDownload fileSource, FileDestination, HostURL, 21, "YOUR_USERNAME_HERE", "YOUR_PASSWORD_HERE"

答案 1 :(得分:0)

O(N)

我现在已经尝试了一切。所以我使用上面的路径。 虽然我手动下载文件,但该网址有效。 [ftp://username:password@ftp.datacentre.com/country_forecast/CWG_ecop_19021012_United-Kingdom.csv]