Upload file via FTP from Excel VBA

后端 未结 5 583
栀梦
栀梦 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:44

    Diego, I've used the code below successfully for years. The code gets files from the host, but I'm sure it can be modified to put files there instead.

    'Start Code
    Set FSO = CreateObject("scripting.filesystemobject")
    
    '**************************************************************************************    '***        Create FTP Action File & Initiate FTP File Transfer
    '**************************************************************************************    VREDET = filename1 'Variable holding name of file to get
    
    F = "C:\Volume\Temp\FTPScript.txt" 'creates the file that holds the FTP commands
    
    Open F For Output As #1
    Print #1, "open ftp.server" 'replace ftp.server with the server address
    Print #1, ID 'login id here
    Print #1, PW 'login password here
    Print #1, "cd " & " Folder1" 'Directory of file location
    Print #1, "cd " & " Folder2" 'Sub-Directory of file location
    Print #1, "ascii"
    Print #1, "prompt"
    'Get the file from the host and save it to the specified directory and filename
    Print #1, "get " & VREDET; " C:\some\directory\" & another-filename & ".CSV"
    Print #1, "disconnect" 'disconnect the session
    Print #1, "bye"
    Print #1, "exit"
    Close #1
    
    'identify folder where ftp resides and execute the FTPScript.txt file
    'vbHide - hides the FTP session
    
    If FSO.FolderExists("C:\Windows\System32") = False Then
        Shell "C:\WINNT\system32\ftp.exe -s:C:\Volume\Temp\FTPScript.txt", vbHide
    Else
        Shell "C:\WINDOWS\system32\ftp.exe -s:C:\Volume\Temp\FTPScript.txt", vbHide
    End If
    'end code
    

提交回复
热议问题