excel vba上传文件到ftp服务器

我在Excel中使用以下脚本来尝试上传文本文件到我的服务器。 由于某种原因,我的脚本失败了。

请有人告诉我我要去哪里错了? 所有的login细节是正确的,我的FTP服务器地址是正确的。 不知道为什么它不会工作?

我将脚本放置在模块中,并将模块分配给活动的xbutton控件

'Open the Internet object Private Declare Function InternetOpen _ Lib "wininet.dll" _ Alias "InternetOpenA" _ (ByVal sAgent As String, _ ByVal lAccessType As Long, _ ByVal sProxyName As String, _ ByVal sProxyBypass As String, _ ByVal lFlags As Long) As Long 'Connect to the network Private Declare Function InternetConnect _ Lib "wininet.dll" _ Alias "InternetConnectA" _ (ByVal hInternetSession As Long, _ ByVal sServerName As String, _ ByVal nServerPort As Integer, _ ByVal sUsername As String, _ ByVal sPassword As String, _ ByVal lService As Long, _ ByVal lFlags As Long, _ ByVal lContext As Long) As Long 'Get a file using FTP Private Declare Function FtpGetFile _ Lib "wininet.dll" _ Alias "FtpGetFileA" _ (ByVal hFtpSession As Long, _ ByVal lpszRemoteFile As String, _ ByVal lpszNewFile As String, _ ByVal fFailIfExists As Boolean, _ ByVal dwFlagsAndAttributes As Long, _ ByVal dwFlags As Long, _ ByVal dwContext As Long) As Boolean 'Send a file using FTP Private Declare Function FtpPutFile _ Lib "wininet.dll" _ Alias "FtpPutFileA" _ (ByVal hFtpSession As Long, _ ByVal lpszLocalFile As String, _ ByVal lpszRemoteFile As String, _ ByVal dwFlags As Long, _ ByVal dwContext As Long) As Boolean 'Close the Internet object Private Declare Function InternetCloseHandle _ Lib "wininet.dll" _ (ByVal hInet As Long) As Integer Sub UploadFTP() Dim hostFile As String Dim INet As Long Dim INetConn As Long Dim Password As String Dim RetVal As Long Dim ServerName As String Dim Success As Long Dim UserName As String Const ASCII_TRANSFER = 1 Const BINARY_TRANSFER = 2 ServerName = "***" UserName = "****" Password = "***" localFile = "P:\TEST.txt" hostFile = "/public_html/TEST.txt" RetVal = False INet = InternetOpen("MyFTP Control", 1&, vbNullString, vbNullString, 0&) If INet > 0 Then INetConn = InternetConnect(INet, ServerName, 0&, UserName, Password, 1&, 0&, 0&) If INetConn > 0 Then Success = FtpPutFile(INetConn, localFile, hostFile, BINARY_TRANSFER, 0&) RetVal = InternetCloseHandle(INetConn) End If RetVal = InternetCloseHandle(INet) End If If Success <> 0 Then MsgBox ("Upload process completed") Else MsgBox "FTP File Error!" End If End Sub 

考虑使用WinSCP; 我发现它更可靠。 在这里看到我的答案为例。