Upload file via FTP from Excel VBA

后端 未结 5 588
栀梦
栀梦 2021-01-14 21:09

Need to upload a file (file.txt) to a server (ftp.server.com) from Excel VBA. (does not have to be necessarily FTP, just need to be able to put the file there and get it bac

5条回答
  •  旧巷少年郎
    2021-01-14 21:38

    The above script is great I used the following commands to upload files as well as log the output to a file which is useful when debugging also it is a common misconception that windows ftp cannot do passive mode the command to go passive is "quote pasv" (I have added this to the script

    Sub FtpFileto()
        Set FSO = CreateObject("scripting.filesystemobject")
        F = "C:\FTPScript.txt"
        ' Create the ftpscript to be run
    
        Open F For Output As #1
        Print #1, "open ftp.server.com" 'replace ftp.server with the server address
        Print #1, "ID" 'login id here
        Print #1, "PWD" 'login password here
        Print #1, "quote pasv" ' passive mode ftp if needed
        Print #1, "cd " & " /dir" 'Directory of file location
        Print #1, "cd " & " subdir" 'Sub-Directory of file location
        Print #1, "ascii"
        Print #1, "prompt"
        'Put the file from the host and save it to the specified directory and filename
        Print #1, "put " & VREDET; """C:\file1.csv"""; ""
        Print #1, "put " & VREDET; """C:\file2.csv"""; ""
        Print #1, "put " & VREDET; """C:\file3.csv"""; ""
        Print #1, "disconnect" 'disconnect the session
        Print #1, "bye"
        Print #1, "exit"
        Close #1
        'Now for the command to upload to the ftpsite and log it to a text file
        ' the trick is to use the standard command shell which allows logging
    
        Shell "cmd /c C:\WINDOWS\system32\ftp.exe -i -s:C:\FTPScript.txt > c:\ftpuploadlog.txt", vbHide
    
        End Sub
    

提交回复
热议问题