在VBA中使用FTP

我编写了一个VBA代码,它根据Excel数据(Websphere MQ Define Job)为IBM主机创build一个带有作业代码的.txt文件。

有可能通过FTP自动传输这个文件到主机将是很酷的。 在这一点上,我通过手动执行此操作:

(评论:LPAR =主机名)

ftp <LPAR> 'user' 'password' put 'dateiname' 

它工作得很好。 但是我不知道如何将这个转换成VBA代码。 我在这里和那里发现了一个类似的问题:

 Public Sub FtpSend() Dim vPath As String Dim vFile As String Dim vFTPServ As String Dim fNum As Long vPath = ThisWorkbook.path vFile = "path" vFTPServ = "<LPAR>" 'Mounting file command for ftp.exe fNum = FreeFile() Open vPath & "\FtpComm.txt" For Output As #fNum Print #1, "user *******" ' your login and password" 'Print #1, "cd TargetDir" 'change to dir on server Print #1, "bin" ' bin or ascii file type to send Print #1, "put " & vPath & "\" & vFile & " " & vFile ' upload local filename to server file Print #1, "close" ' close connection Print #1, "quit" ' Quit ftp program Close Shell "ftp -n -i -g -s:" & vPath & "\FtpComm.txt " & vFTPServ, vbNormalNoFocus SetAttr vPath & "\FtpComm.txt", vbNormal Kill vPath & "\FtpComm.txt" End Sub 

我不确定是否完全理解代码。 我想我创build一个虚拟文件,FtpComm.txt与用户数据和内容,并使用此文件打开连接并发送数据。

它不知何故,直到这一点

 *SetAttr vPath & "\FtpComm.txt", vbNormal* 

那里我得到错误

运行时错误:55 – 文件已经打开。

到LPAR的连接就是在这一点上设置的。 但是“SetAttr …”是做什么的? 这是input完成的地方吗? 我该怎么办?

两件事情。

首先,从fNum = FreeFile()获得的文件编号#fNum下打开一个文件,然后假设这将返回1,如Print #1等:

 Open vPath & "\FtpComm.txt" For Output As #fNum Print #1, "user *******" ' your login and password" 

将所有Print #1更改为Print #fNum

其次,你打开你的文件的I / O,但不要closures它。 所以当然,当你尝试修改它的属性或删除它时,系统会抱怨它已经打开并且正在使用中。 解决方法是closures文件 – 只要您完成写入/读取文件,您应该始终执行此操作。

 Close #fNum ' add this line ' Can now manipulate the file without risking conflict Shell "ftp -n -i -g -s:" & vPath & "\FtpComm.txt " & vFTPServ, vbNormalNoFocus SetAttr vPath & "\FtpComm.txt", vbNormal Kill vPath & "\FtpComm.txt" 

至于你的代码在高层次上做了什么:如果你在命令行inputftp -help ,你会看到那些-n -i -g -s标志的含义。 所以是的,你正在写FTP命令来文件FtpComm.txt ,然后通过-s标志提供该文件到ftp

 -s:filename Specifies a text file containing FTP commands; the commands will automatically run after FTP starts. 

你走在正确的道路上,跟随让 – 弗朗索瓦的答案应该把你带到那里。

如果你想要的东西你可以剪切和粘贴,你可以尝试下面的代码。 这是我几年前写的东西的一个变种。

最重要的警告是,一旦你shell命令FTP,你不知道文件是否真正转移,直到你手动检查。

但是,如果你只限于严格的VBA ,那么这是一条路:

 Option Explicit Const FTP_ADDRESS = "ftp.yourdestination.com" Const FTP_USERID = "anon" Const FTP_PASSWORD = "anon" Sub Macro1() If Not SendFtpFile_F() Then MsgBox "Could not ftp file" Else MsgBox "Sent" End If End Sub Function SendFtpFile_F() As Boolean Dim rc As Integer Dim iFreeFile As Integer Dim sFTPUserID As String Dim sFTPPassWord As String Dim sWorkingDirectory As String Dim sFileToSend As String Const FTP_BATCH_FILE_NAME = "myFtpFile.ftp" Const INCREASED_BUFFER_SIZE = 20480 SendFtpFile_F = False sWorkingDirectory = "C:\YourWorkingDirectory\" sFileToSend = "NameOfFile.txt" On Error GoTo FtpNECAFile_EH 'Kill FTP process file if it exists If Dir(sWorkingDirectory & FTP_BATCH_FILE_NAME) <> "" Then Kill sWorkingDirectory & FTP_BATCH_FILE_NAME End If 'Create FTP process file iFreeFile = FreeFile Open sWorkingDirectory & FTP_BATCH_FILE_NAME For Output As #iFreeFile Print #iFreeFile, "open " & FTP_ADDRESS Print #iFreeFile, FTP_USERID Print #iFreeFile, FTP_PASSWORD Print #iFreeFile, "mput " & sWorkingDirectory & sFileToSend Print #iFreeFile, "quit" Close #iFreeFile 'Shell command the FTP file to the server Shell "ftp -i -w:20480 -s:" & sWorkingDirectory & FTP_BATCH_FILE_NAME SendFtpFile_F = True GoTo FtpNECAFile_EX FtpNECAFile_EH: MsgBox "Err", Err.Name FtpNECAFile_EX: Exit Function End Function 

注意:单步执行代码时,可以在即时窗口中键入以下命令,然后将结果复制/粘贴到命令窗口中:

 ? Shell "ftp -i -w:20480 -s:" & sWorkingDirectory & FTP_BATCH_FILE_NAME 

SetAttr用于设置文件(或文件夹)的属性。 在你的情况下,它将其设置回正常(所以不能隐藏,只读等)。 但是,由于您的文件仍然是从“打开输出”调用打开,它将失败,并出现“文件已打开”错误。

我没有看到为什么调用SetAttr函数的任何原因,因为你不在其他地方改变属性。 所以随时删除它,看看它是否仍然有效。

在第一个例子中,在句子之后

 Print #1, "quit" ' Quit ftp program Close 

您必须closures用于与句子连接的文件:

 Close #fNum 

因为这个文件目前正在使用。