通过FTP从Excel VBA上传文件

Die*_*tro 2 ftp excel vba

需要从Excel VBA将文件(file.txt)上载到服务器(ftp.server.com).(不一定是FTP,只需要能够将文件放在那里并将其取回,我在GoDaddy共享主机上有一台服务器)

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

ftp -s:script.txt
Run Code Online (Sandbox Code Playgroud)

script.txt:

open ftp.server.com
USER
PASS
lcd c:\
put file.txt
disconnect
bye
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

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

谷歌告诉我,我需要进入被动模式,但命令行ftp.exe客户端不允许这样做.

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

我有更简单的替代FTP吗?

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

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

Mar*_*ryl 6

如果您无法使用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""")
Run Code Online (Sandbox Code Playgroud)

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

open ftp://user:password@example.com/
put C:\path\file.txt /path/
exit
Run Code Online (Sandbox Code Playgroud)

您可以将命令放入脚本文件并使用/script= 命令行参数运行脚本,类似于ftp -s:,而不是/command.


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

您甚至可以让WinSCP GUI为您生成FTP上载脚本.


WinSCP默认为被动模式.

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

open ftpes://user:password@example.com/
Run Code Online (Sandbox Code Playgroud)

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


(我是WinSCP的作者)


Jer*_*emy 5

迭戈,多年来,我已经成功使用了下面的代码。该代码从主机获取文件,但我确定可以对其进行修改以将文件放到那里。

'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
Run Code Online (Sandbox Code Playgroud)