我怎样才能下载使用FTP数据与VBA电子表格为Microsoft Excel

问题描述 投票:0回答:2

我发现下面的代码我开始了与开发自动化的方式来从FTP站点下载文件。然而,它没有失败,我可以解决任何错误每次。

我在Excel VBA中一个新手,所以一些帮助,将不胜感激。

我曾尝试在网上,并在这里寻找在堆栈溢出,但我could'nt算出这个对我自己

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
excel vba ftp
2个回答
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"

0
投票
Dim HostURL, fileSource, FileDestination As String
    HostURL = "ftp.datacentre.com"
    fileSource = "/country_forecast/CWG_ecop_19021012_United-Kingdom.csv"
    FileDestination = "C:\Development\Test1\newFile.txt"

FtpDownload fileSource, FileDestination, HostURL, 21, "username", "password"

我现在已经尝试了一切。所以我用上面的路径。该网址的作品,虽然我我手动下载该文件。 [ftp://username:[email protected]/country_forecast/CWG_ecop_19021012_United-Kingdom.csv]

© www.soinside.com 2019 - 2024. All rights reserved.