Bonjour,

Je tente d'uploader un fichier texte sur un serveur FTP en VBA sous Excel 2010 en utilisant le code suivant (qui s'inspire du tuto https://access.developpez.com/source...hiers#EnvoiFTP) :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
52
53
54
55
56
57
58
59
 
Private Declare Function InternetCloseHandle Lib "wininet.dll" _
  (ByVal hInet As Long) As Integer
 
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
 
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
 
Private Declare Function FtpSetCurrentDirectory Lib "wininet.dll" Alias _
"FtpSetCurrentDirectoryA" (ByVal hFtpSession As Long, _
ByVal lpszDirectory As String) As Boolean
 
Private Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" _
(ByVal hConnect As Long, ByVal lpszRemoteFile As String, _
ByVal lpszNewFile As String, ByVal fFailIfExists As Long, _
ByVal dwFlagsAndAttributes As Long, ByVal dwFlags As Long, _
ByRef dwContext As Long) As Boolean
 
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 Declare Function FtpGetCurrentDirectory Lib "wininet.dll" Alias "FtpGetCurrentDirectoryA" ( _
ByVal hFtpSession As Long, _
ByVal lpszDirectory As String, _
ByRef lpdwCurrentDirectory As Long) As Boolean
 
Sub Dashboard_export()
 
    Dim blok As Boolean
    Dim errorCode As Long
 
    'Gestion FTP
    Dim HwndConnect As Long
    Dim HwndOpen As Long
 
    'Ouvre Internet
    HwndOpen = InternetOpen("SiteWeb", 0, vbNullString, vbNullString, 0)
    'Connection au site ftp
    HwndConnect = InternetConnect(HwndOpen, "monftp", 21, "monidentifiant", "monmotdepasse", 1, 0, 0)
 
    'positionnement du curseur dans le répertoire
    blok = FtpSetCurrentDirectory(HwndConnect, "/tmp")
 
    ' Téléchargement du fichier sur le FTP
    blok = FtpPutFile(HwndConnect, "C:\test\test.txt", "coucou.txt", &H0, 0)
    errorCode = Err.LastDllError
 
    InternetCloseHandle HwndConnect 'Ferme la connection
    InternetCloseHandle HwndOpen 'Ferme internet
 
End Sub
Lorsque j'accède au FTP, je vois bien qu'un fichier "coucou.txt" a été créé mais il est vide !
FtpSetCurrentDirectory renvoit bien "True" par contre FtpPutFile renvoit "False" même si un fichier "coucou.txt" est bien créé.
Je ne suis pas sur de bien utiliser "Err.LastDllError" mais j'ai un code renvoyé égal à 12002 mais ce dernier ne me renseigne pas beaucoup sur mon problème...

Auriez-vous une idée de ce qui ne va pas ?

Merci par avance pour votre aide