调用Shell时,VBA代码不执行FTP下载或写入日志

时间:2018-03-07 00:07:15

标签: vba excel-vba ftp excel

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

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

首先问题是我试图为指定here by jacouh指定目标文件和ftp日志的目录,因此脚本不会在DOS中运行。

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

两个问题:

  1. 有没有办法在VBA中查看日志文件?
  2. 任何人都会看到一些显而易见的事情我可能做错了吗?
  3. 以下是代码:

    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
    

    感谢。

1 个答案:

答案 0 :(得分:0)

如果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"