从Excel VBA通​​过FTP上传文件

需要从Excel VBA上传文件(file.txt)到服务器(ftp.server.com)。 (不一定是FTP,只需要能够把文件放在那里,并把它拿回来,我有一个服务器上的GoDaddy共享主机)

我试过的是运行这个脚本:

ftp -s:script.txt 

script.txt:

 open ftp.server.com USER PASS lcd c:\ put file.txt disconnect bye 

我得到的错误是:

425无法打开到端口53637的数据连接:连接超时

谷歌告诉我,我需要去被动模式,但命令行ftp.exe客户端不允许。

任何人都知道任何免费的(开源)命令行FTP客户端,允许被动模式?

我有一个更容易的替代FTP?

有没有更好的方法来通过VBA上传文件(没有命令行解决方法)?

我正在考虑使用DROPBOX(但我真的不想在所有需要该程序的工作站上安装此程序)。

如果您不能使用Windows ftp.exe (特别是因为它不支持被动模式和TLS / SSL),您可以使用另一个命令行FTP客户端。

例如,要使用WinSCP脚本上传文件,请使用:

 Call Shell( _ "C:\path\WinSCP.com /log=C:\path\excel.log /command " & _ """open ftp://user:password@example.com/"" " & _ """put C:\path\file.txt /path/"" " & _ """exit""") 

为了方便阅读,上面运行这些WinSCP命令:

 open ftp://user:password@example.com/ put C:\path\file.txt /path/ exit 

您可以将这些命令放到脚本文件中,并使用/script= command-line参数运行脚本,与ftp -s:相似,而不是/command


请参阅将Windows FTP脚本转换为WinSCP脚本的指南 。

你甚至可以让WinSCP GUI为你生成FTP上传脚本 。


WinSCP默认为被动模式。

您也可以使用FTPS(TLS / SSL) :

 open ftpes://user:password@example.com/ 

或者,您可以使用通过COM从VBA代码的WinSCP .NET程序集 。


(我是WinSCP的作者)

迭戈,我已经使用了下面的代码多年。 代码从主机获取文件,但我相信它可以被修改,把文件放在那里。

 'Start Code Set FSO = CreateObject("scripting.filesystemobject") '************************************************************************************** '*** Create FTP Action File & Initiate FTP File Transfer '************************************************************************************** VREDET = filename1 'Variable holding name of file to get F = "C:\Volume\Temp\FTPScript.txt" 'creates the file that holds the FTP commands Open F For Output As #1 Print #1, "open ftp.server" 'replace ftp.server with the server address Print #1, ID 'login id here Print #1, PW 'login password here Print #1, "cd " & " Folder1" 'Directory of file location Print #1, "cd " & " Folder2" 'Sub-Directory of file location Print #1, "ascii" Print #1, "prompt" 'Get the file from the host and save it to the specified directory and filename Print #1, "get " & VREDET; " C:\some\directory\" & another-filename & ".CSV" Print #1, "disconnect" 'disconnect the session Print #1, "bye" Print #1, "exit" Close #1 'identify folder where ftp resides and execute the FTPScript.txt file 'vbHide - hides the FTP session If FSO.FolderExists("C:\Windows\System32") = False Then Shell "C:\WINNT\system32\ftp.exe -s:C:\Volume\Temp\FTPScript.txt", vbHide Else Shell "C:\WINDOWS\system32\ftp.exe -s:C:\Volume\Temp\FTPScript.txt", vbHide End If 'end code 

上面的脚本是伟大的我用下面的命令上传文件以及输出到一个文件,这是有用的,当debugging也是一个常见的误解,即Windows FTP不能做被动模式命令去被动是“引用” (我已经添加了这个脚本

 Sub FtpFileto() Set FSO = CreateObject("scripting.filesystemobject") F = "C:\FTPScript.txt" ' Create the ftpscript to be run Open F For Output As #1 Print #1, "open ftp.server.com" 'replace ftp.server with the server address Print #1, "ID" 'login id here Print #1, "PWD" 'login password here Print #1, "quote pasv" ' passive mode ftp if needed Print #1, "cd " & " /dir" 'Directory of file location Print #1, "cd " & " subdir" 'Sub-Directory of file location Print #1, "ascii" Print #1, "prompt" 'Put the file from the host and save it to the specified directory and filename Print #1, "put " & VREDET; """C:\file1.csv"""; "" Print #1, "put " & VREDET; """C:\file2.csv"""; "" Print #1, "put " & VREDET; """C:\file3.csv"""; "" Print #1, "disconnect" 'disconnect the session Print #1, "bye" Print #1, "exit" Close #1 'Now for the command to upload to the ftpsite and log it to a text file ' the trick is to use the standard command shell which allows logging Shell "cmd /c C:\WINDOWS\system32\ftp.exe -i -s:C:\FTPScript.txt > c:\ftpuploadlog.txt", vbHide End Sub