从Excel连接到FTP以自动文件共享(VBA初学者)

我是一名初学者,也是Excel VBA的新手,但是我试图通过连接到Excel来自动化FTP(WinSCP)中的一些文件共享,也许会创build一个有用的macros。 在FTP中,我进入会话>生成会话URL /代码>脚本(脚本文件),下面的代码在那里:

open ftp://myUsername:myPassword@theHostname/ # Your command 1 # Your command 2 exit 

我假设开放线将Excel连接到FTP。 我参考了这个网站的代码,把它放到'#command'区域: https : //www.mrexcel.com/forum/excel-questions/261043-connecting-ftp-excel.html

 open ftp://myUsername:myPassword@theHostname/ Option Explicit Sub FtpTest() MsgBox fnDownloadFile("ftp://yoursite", "username", "password", _ "The name of your file", _ "C:\The name of your file to save as") End Sub Function fnDownloadFile(ByVal strHostName As String, _ ByVal strUserName As String, _ ByVal strPassWord As String, _ ByVal strRemoteFileName As String, _ ByVal strLocalFileName As String) As String '// Set a reference to: Microsoft Internet Transfer Control '// This is the Msinet.ocx Dim FTP As Inet 'As InetCtlsObjects.Inet Set FTP = New Inet 'InetCtlsObjects.Inet On Error GoTo Errh With FTP .URL = strHostName .Protocol = 2 .UserName = strUserName .Password = strPassWord .Execute , "Get " + strRemoteFileName + " " + strLocalFileName Do While .StillExecuting DoEvents Loop fnDownloadFile = .ResponseInfo End With Xit: Set FTP = Nothing Exit Function Errh: fnDownloadFile = "Error:-" & Err.Description Resume Xit End Function exit 

我做了这个网站说去VBA编辑器>工具>参考,并检查微软互联网控制。

1)我使用的代码是否正确? 我把它放在正确的区域(在“#命令”区域)吗? 现在我把整个代码放在一个命令button中,但是当我点击它时,它只是给我一个高亮显示第一行的语法错误:Private Sub CommandButton1_Click())

2)我是否将Msgbox留在第三行,等待用户input,还是填写我的用户名/密码/主机名? (我在VBA中的function还不是很好)如果我在代码中填充它,那么因为我没有访问一个网站,所以我为“yoursite”值做了什么?

对不起,我很困惑:(任何帮助将是伟大的,并提前谢谢你!

我认为你应该看看这里 – 在这里显示的Inet对象的Excel VBA参考如何在vba中添加INet对象的参考。 此外,当你只是想testing代码是否工作,而不是将macros分配给button等,如果你使用“function”,那么当你去工作表单元格,并开始键入= fnDown …你应该看到你的macros – 那里你可以把你的function参数。 但首先你必须注意对Inet的引用。

此链接也可能会有所帮助: VBA Excel和FTP与MSINET.OCX和Inettypes