I have had these functions working now for a few months, so I figure they are good enough to share with the world. If you are interested in this topic, I am sure you have read 1DMF's FAQs on FTP'ing files from access. This is a great function, but I ran into problems uploading encrypted files (and sometimes zipped Access db's) using it. There also is not a function to download, which I needed.
So, I had most of the work done for me, but needed to come up with two more functions (one to upload files using FTPPutFile rather then InternetWriteFile, and one to download the files). One of these days I plan to add one for deleting files as well.
In case you have not read 1DMF's FAQ on this subject, look here for the declaration section:
Function FTPDownloadFile(ByVal HostName As String, _ ByVal UserName As String, _ ByVal Password As String, _ ByVal LocalFileName As String, _ ByVal RemoteFileName As String, _ ByVal sDir As String, _ ByVal sMode As String) As Boolean
' Declare variables Dim hConnection, hOpen ' Used For Handles Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject") 'check for existence of destination file If fso.FileExists(LocalFileName) Then
'delete it if found VBA.Kill LocalFileName
End If
' Open Internet Connecion hOpen = InternetOpen("FTP", 1, "", vbNullString, 0)
' Set Download Flag to True FTPDownloadFile = True
' Download File If FTPGetFile(hConnection, RemoteFileName, & _ LocalFileName, False, 1, 0, 1) = False Then MsgBox "Download - Failed At FTPGetFile!" FTPDownloadFile = False
End If
'ensure file is not saved as read-only SetAttr LocalFileName, vbNormal + vbArchive
' Close Internet Connection Call InternetCloseHandle(hOpen) Call InternetCloseHandle(hConnection)
End Function
And now the upload function:
CODE
Function FTPUploadFile(ByVal HostName As String, _ ByVal UserName As String, _ ByVal Password As String, _ ByVal LocalFileName As String, _ ByVal RemoteFileName As String, _ ByVal sDir As String, _ ByVal sMode As String) As Boolean
'If this function is not working, 'try playing with Flags and Context '(2nd and 3rd parameters in FTPPutFile)
' Declare variables Dim hConnection, hOpen ' Used For Handles
' Open Internet Connecion hOpen = InternetOpen("FTP", 1, "", vbNullString, 0)
' Download File If FtpPutFile(hConnection, LocalFileName, & _ RemoteFileName, 0, 1) = False Then MsgBox "Download - Failed At FTPPutFile!" FTPUploadFile = False
End If
' Close Internet Connection Call InternetCloseHandle(hOpen) Call InternetCloseHandle(hConnection)
End Function
Much thanks go to 1DMF for his FAQs, had I not read them I would still be creating FTP scripts on the fly and running them through command window (this was not very reliable).
Please let me know if you have any suggestions to improve these functions or questions.