【问题标题】:VBA Code Does Not Execute FTP Download or Write Log when call Shell调用 Shell 时 VBA 代码不执行 FTP 下载或写入日志
【发布时间】:2018-03-08 04:22:51
【问题描述】:

我使用ray's 的答案开发了一个将脚本文件写入目录然后在Shell 中运行脚本的过程。

代码不会引发错误,但不会下载文件或将 FTP 日志写入我指定的文本文件。

最初的问题是我试图为目标文件和 ftp 日志指定一个目录,正如 here by jacouh 指出的那样,因此脚本无法在 DOS 中运行。

我相信我纠正了这个错误,现在可以使用 Shell 命令中使用的确切命令行在 DOS 窗口中运行脚本。但是,它仍然不能在 VBA 中运行。

两个问题:

  1. 有没有办法在 VBA 中查看日志文件?
  2. 有人看到我可能做错了什么明显的事情吗?

代码如下:

Function getFilesViaFTP(fileToGet As String, fileToWrite As String) As Boolean
Dim rc As Integer
Dim iFreeFile As Integer
Dim sFTPUserID As String
Dim sFTPPassWord As String
Dim sWorkingDirectory As String
Dim sFtpLogFile As String

Dim myCurrentDirectory As String

Const FTP_BATCH_FILE_NAME = "myFTPscript.txt"
Const INCREASED_BUFFER_SIZE = 20480

Dim fileNameForDownload As String
fileNameForDownload = fileToWrite

Dim fullGetCommand As String
Dim fullShellCommand As String

myCurrentDirectory = CurDir()

getFilesViaFTP = False

sWorkingDirectory = "K:\Users\WWTP Computer\Documents\DMR's\ftpDownloads\"

sFtpLogFile = "ftp_Log" & "_" & year(Date) & "_" & month(Date) & "_" & day(Date) & ".txt"


On Error GoTo EncounteredErrorInProc

'Kill FTP process file if it exists
If Dir(sWorkingDirectory & FTP_BATCH_FILE_NAME) <> "" Then
    Kill sWorkingDirectory & FTP_BATCH_FILE_NAME
End If

If Dir(sWorkingDirectory & sFtpLogFile) <> "" Then
    Kill (sWorkingDirectory & sFtpLogFile)
End If

Dim i As Integer
Dim whereIsDot As Integer
i = 1

'Change target file name if one already exists
Do While Dir(sWorkingDirectory & fileNameForDownload) <> ""
    whereIsDot = InStr(fileToWrite, ".")
    fileNameForDownload = Left(fileToWrite, whereIsDot - 1) & "_" & i & ".txt"
    Debug.Print fileNameForDownload
Loop

fullGetCommand = "get " & fileToGet & " " & fileNameForDownload

Debug.Print fullGetCommand
Debug.Print sFtpLogFile

'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, "lcd " & sWorkingDirectory
Print #iFreeFile, fullGetCommand
Print #iFreeFile, "quit"
Close #iFreeFile

'Shell command the FTP file to the server
ChDir sWorkingDirectory
Debug.Print "Current Directory = " & CurDir()
fullShellCommand = "ftp -i -w:20480 -s:" & FTP_BATCH_FILE_NAME & ">" & sFtpLogFile
Debug.Print fullShellCommand

Shell fullShellCommand

getFilesViaFTP = True
ChDir myCurrentDirectory
Debug.Print "New Current Directory = " & CurDir()

GoTo NoErrorsInProc

EncounteredErrorInProc:
    MsgBox "Err", Err.Name

 NoErrorsInProc:

Exit Function
End Function

谢谢。

【问题讨论】:

  • 所以您的具体问题是您执行Shell 命令的部分?其余代码运行良好?
  • 似乎是。 VBA 在运行 Shell 时不提供日志。我会在下面尝试你的建议。谢谢。

标签: vba excel ftp


【解决方案1】:

如果Shell 部分是唯一的问题,您可以做的是创建一个批处理文件来执行您的FTP 命令。比如:

Dim ff As Integer

ff = FreeFile()
Open "runFTP.bat" For Output As #ff
Print #ff, fullshellcommand '/* what you generated in your code */
Close #ff

Shell "runFTP.bat"

【讨论】:

  • 成功了!我从来没有想过要启动另一个 Shell 来让原来的 shell 工作!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-23
相关资源
最近更新 更多