通过Excel从Excel上传文件VBA
需要从Excel VBA上传文件(file.txt)到服务器(ftp.server.com)。 (不必一定是FTP,只需要能够把文件放在那里,并得到它回来,我有一个服务器上的GoDaddy共享托管)通过Excel从Excel上传文件VBA
我试过的是运行此脚本:
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(但我真的不想在所有需要该程序的工作站上安装此程序)。
http://winscp.net是免费的,可脚本化,支持被动模式,绝对是优秀的。
我无法理解为什么这是公认的答案? 这家伙在excel里面问VBA,而不是桌面软件! 对于桌面FileZilla是最好和最常用的一个,并在每个平台上工作:Windows,Linux,Mac:https://filezilla-project.org/ –
@NikolayIvanov:“Filezilla是最好的”是一个非常主观的观点。 AFAIK,FZ不是可脚本化的,这使得它在许多情况下不可用。这是一个事实:-) –
@NikolayIvanov:“这个家伙” - 正如你所说 - 可能比你更了解答案。我的建议恰恰满足了他的需求。 –
迭戈,我多年来成功地使用了下面的代码。该代码从主机获取文件,但我确信可以修改它以将文件放在那里。
'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
原生Windows FTP不支持被动模式,当你需要它的时候,这是个不错的选择。 –
thnx @Jeremy,但我真的需要被动模式 –
不是答案,直到它回答问题 –
上述脚本是太好了,我用下面的命令来上传文件,以及日志的输出文件,该文件是有用的,当调试也实在是一种常见的误解窗户FTP不能做被动模式下的命令去被动为“引用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
[Windows ftp.exe不支持被动模式](http://stackoverflow.com/q/18643542/850848 )。使用'quote pasv'无法帮助。 –
如果您不能使用Windows ftp.exe
(特别是因为it does not support the passive mode和TLS/SSL),你可以使用另一个命令行FTP客户端。
例如上传使用WinSCP scripting文件,请使用:
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 parameter运行脚本,与ftp -s:
类似,而不是/command
。
请参阅指南Converting Windows FTP script to WinSCP script。你可以为你提供WinSCP GUI generate the FTP upload script。
WinSCP默认为被动模式。
您还可以使用FTPS (TLS/SSL):
open ftpes://user:[email protected]/
或者您可以使用WinSCP .NET assembly via COM从VBA代码。
(我的WinSCP的作者)
如果VBA允许一个HTTP请求时,你可以发布你的服务器上的文件,一个脚本,然后让脚本处理它。 – xbonez