我无法让下面的代码工作(来自Using FTP in VBA的答案)。我在WinSCP的FTP客户端,我不是100%确定如何使用代码。当我运行宏时,我得到了成功的MsgBox“已发送”弹出窗口,我看到一个名为FTP_BATCH_FILE_NAME的文件被创建,其中包含以下信息:
"open " & FTP_ADDRESS
FTP_USERID
FTP_PASSWORD
"mput " & sWorkingDirectory & sFileToSend
quit
发布代码的人注意到 -
“在单步执行代码时,您可以在即时窗口中键入以下命令,然后将结果复制/粘贴到命令窗口中:”
? Shell "ftp -i -w:20480 -s:" & sWorkingDirectory & FTP_BATCH_FILE_NAME
但是当我逐步执行代码时,在黄色突出显示在Excel中传递此行之后,我输入此行并收到错误“?”,sWorkingDirectory和FTP_BATCH_FILE_NAME未被识别为内部或外部命令。
我认为在Cmd行输入后我可以将FTP_BATCH_FILE_NAME的内容输入sFileToSend,但没有任何内容放入sFileToSend。
Option Explicit
Const FTP_ADDRESS = myHostAddr
Const FTP_USERID = myUsername
Const FTP_PASSWORD = myPassword
Sub Macro1()
If Not SendFtpFile_F() Then
MsgBox "Could not (send/get?) 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 = "a file in FTP I want to get.ftp"
Const INCREASED_BUFFER_SIZE = 20480
SendFtpFile_F = False
sWorkingDirectory = "C:\Users\...\Documents\"
sFileToSend = "test.txt" 'is this the file I want to put it into?
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