FTP upload file in VBA

lotsbg

Board Regular
Joined
Mar 8, 2004
Messages
110
Hi I'm trying to upload a file within excel 2003 and I want to do it from VBA.

so for example I would ahve the following variables

username: username
password: password
ftp: ftp.website.com
filetoupload: filename
webfolder to upload to: ftp.website.com/uploadfilehere/

If anyone can help with how to do this from within VBA that would be great.
Thanks
Jon
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
How about something like this? It's the only way I can think of doing it but it's horribly insecure as the temp file/workbook contains the password in plaintext. Personally I wouldn't do this, I'd look at setting up AD authentication on the FTP server.

It takes it's parameters from the workbook (B1:B5).

Code:
Option Explicit

Sub FTPFile()
On Error GoTo Err_FTPFile

    Dim sHost As String
    Dim sUser As String
    Dim sPass As String
    Dim sSrc As String
    Dim sDest As String
    Dim sFTPCmds As String
    Dim iFNum As Integer

    'Build up the necessary parameters
    sHost = Sheet1.Cells(1, "B").Value
    sUser = Sheet1.Cells(2, "B").Value
    sPass = Sheet1.Cells(3, "B").Value
    sSrc = Sheet1.Cells(4, "B").Value
    sDest = Sheet1.Cells(5, "B").Value

    'Write the FTP commands to a text file
    iFNum = FreeFile
    sFTPCmds = Environ("TEMP") & "\" & "FTPCmd.tmp"
    Open sFTPCmds For Output As #iFNum
        Print #iFNum, "op " & sHost
        Print #iFNum, "user " & sUser & " " & sPass
        Print #iFNum, "cd " & sDest
        Print #iFNum, "put " & sSrc
        Print #iFNum, "bye"
    Close #iFNum

    'Upload the file
    Shell Environ("WINDIR") & "\System32\ftp.exe -n -s:" & sFTPCmds

Exit_FTPFile:
    On Error Resume Next
    Close #iFNum
    'Delete the temp FTP command file
    Kill sFTPCmds
    Exit Sub

Err_FTPFile:
    MsgBox Err.Number & " - " & Err.Description, vbOKOnly, "Error"
    Resume Exit_FTPFile

End Sub
 
Upvote 0
This worked for me after a long time of trial and error
I had to add the following line :
VBA Code:
Application.Wait (Now + TimeValue("00:00:01"))
before and after
VBA Code:
Shell Environ("WINDIR") & "\System32\ftp.exe -n -s:" & sFTPCmds
I am guessing I needed to slow the macro down(?)

My values in B1-B5 are
host name (ie mysite.co.uk)
Username
Password
source file (ie C:\folder_name\folder_name\file_name)
Destination (ie www/host name/folder_name - file name not required - this structure may vary for different hosts - you have to check the file structure on your server)

I hope this helps and I congratulate rjp
You can password protect the Excel macro file and then it would be secure (?)
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,731
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top