1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| Const FTP_TRANSFER_TYPE_UNKNOWN = &H0 'Type de transfert FTP auto
Const FTP_TRANSFER_TYPE_ASCII = &H1 'Type de transfert FTP ASCII
Const FTP_TRANSFER_TYPE_BINARY = &H2 'Type de transfert FTP Binaire
Const INTERNET_DEFAULT_FTP_PORT = 21 'Port FTP par défaut
Const INTERNET_SERVICE_FTP = 1 'Type de connextion Internet = FTP
Const INTERNET_FLAG_PASSIVE = &H8000000 'FTP Mode Passif
Const INTERNET_OPEN_TYPE_PRECONFIG = 0 'Connexion selon la config en base de registre
Const INTERNET_OPEN_TYPE_DIRECT = 1 'Connexion directe
Const INTERNET_OPEN_TYPE_PROXY = 3 'Connexion via proxy (à nommer)
Const INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY = 4 'Connexion selon la config en base de registre, mais pas de connexion auto au proxy
Const MAX_PATH = 256 'Taille maximum du buffer pour les chaînes de caractères
Const PassiveConnection As Boolean = True 'Pour définir le serveur FTP en mode passif
'Pour fermer tout handle de type Internet
Private Declare Function InternetCloseHandle Lib "wininet" (ByRef hInet As Long) As Long
'Pour créer la connexion de type Internet
Private Declare 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
'Pour créer une ouverture Internet sur laquelle on placera la connexion
Private Declare 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
'Pour mettre un fichier via FTP sur un serveur
Private Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hConnect As Long, ByVal lpszLocalFile As String, ByVal lpszNewRemoteFile As String, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
Private Sub CommandButton1_Click()
Dim hOpen As Long
Dim hConnection As Long
Dim sLocalFileName As String
Dim sLocalTempFileName As String
Dim sDistantFileName As String
hOpen = InternetOpen("Mettre le nom de l'appli ou rien", INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
hConnection = InternetConnect(hOpen, "nom du serveur", INTERNET_DEFAULT_FTP_PORT, "login", "mot de passe", INTERNET_SERVICE_FTP, IIf(PassiveConnection, INTERNET_FLAG_PASSIVE, 0), 0)
sLocalFileName = ThisWorkbook.Path & "\" & ThisWorkbook.Name
sLocalTempFileName = sLocalFileName & ".tmp"
sDistantFileName = "/apps/kycUAT/" & ThisWorkbook.Name
ThisWorkbook.SaveAs sLocalTempFileName
FileSystem.Kill sLocalFileName
ThisWorkbook.SaveAs sLocalFileName
If FtpPutFile(hConnection, sLocalTempFileName, sDistantFileName, FTP_TRANSFER_TYPE_BINARY, 0) Then
MsgBox "Transfert FTP de " & sLocalFileName & " réussi"
Else
MsgBox "Transfert FTP de " & sLocalFileName & " raté"
End If
FileSystem.Kill sLocalTempFileName
InternetCloseHandle hConnection
InternetCloseHandle hOpen
End Sub |
Partager