通过FTP从Excel VBA上传文件

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

需要从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(但我真的不想在所有需要该程序的工作站上安装此程序)。

excel vba ftp
4个回答
6
投票

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

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

Call Shell( _
    "C:\path\WinSCP.com /log=C:\path\excel.log /command " & _
    """open ftp://user:[email protected]/"" " & _
    """put C:\path\file.txt /path/"" " & _
    """exit""")

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

open ftp://user:[email protected]/
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:[email protected]/

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


(我是WinSCP的作者)


4
投票

迭戈,我已成功使用下面的代码多年。 代码从主机获取文件,但我确信可以修改它以将文件放在那里。

'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

3
投票

http://winscp.net是免费的,可编写脚本的,支持被动模式,绝对是优秀的。


-1
投票

上面的脚本很棒我使用以下命令上传文件以及将输出记录到一个文件中,这在调试时也很有用,这是一个常见的误解,即windows ftp无法执行被动模式,命令被动为“quote pasv” (我已将此添加到脚本中

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
© www.soinside.com 2019 - 2024. All rights reserved.