Tuesday, October 31, 2023

VBA FTP

  Declare PtrSafe Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" ( _

        ByVal hInternetSession As Long, ByVal sServerName As String, _

        ByVal nServerPort As Integer, ByVal sUserName As String, _

        ByVal sPassword As String, ByVal lService As Long, _

        ByVal lFlags As Long, ByVal lContext As Long) As Long

    Declare PtrSafe Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" ( _

        ByVal sAgent As String, ByVal lAccessType As Long, _

        ByVal sProxyName As String, _

        ByVal sProxyBypass As String, ByVal lFlags As Long) As Long

    Declare PtrSafe Function FtpSetCurrentDirectory Lib "wininet.dll" Alias _

     "FtpSetCurrentDirectoryA" (ByVal hFtpSession As Long, _

        ByVal lpszDirectory As String) As Boolean

    Declare PtrSafe Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" ( _

        ByVal hConnect As Long, _

        ByVal lpszLocalFile As String, _

        ByVal lpszNewRemoteFile As String, _

        ByVal dwFlags As Long, _

        ByRef dwContext As Long) As Boolean


Sub simpleFtpFileUpload()


    Dim ftp, FTP_PORT, user, password, loc_file, remote_file, ftp_folder As Variant

    'ftp_folder = "/EXPORT"

    'loc_file = ThisWorkbook.Path & "\readme.txt"

    'remote_file = ftp_folder & "/readme.txt"

    loc_file = "d:\2.jpg"

    remote_file = "2.jpg"

    FTP_PORT = "21"

    user = "ajay"

    password = "ajay"

    ftp = "192.168.0.86"


    Internet_OK = InternetOpen("", 1, "", "", 0)

    If Internet_OK Then

        FTP_OK = InternetConnect(Internet_OK, ftp, FTP_PORT, user, password, 1, 0, 0) ' INTERNET_DEFAULT_FTP_PORT or port no

        If FtpSetCurrentDirectory(FTP_OK, "/") Then

            Success = FtpPutFile(FTP_OK, loc_file, remote_file, FTP_TRANSFER_TYPE_BINARY, 0)

        End If

    End If

    If Success Then

        Debug.Print "ftp success ;)"

        MsgBox "ftp success ;)"

    Else

        Debug.Print "ftp failure :("

        MsgBox "ftp failure :("

    End If

End Sub



//////////////////////////////////////

CR: https://stackoverflow.com/questions/7737691/upload-file-via-ftp-from-excel-vba