Bonjour,

Je dois récupérer des fichiers sur un ftp avec excel sous vba. J'ai déjà adapté une bonne partie de code que j'ai trouvé sur internet mais j'ai encore quelques soucis.

En effet, la fonction FtpGetFile utilisée à la fin du code me renvoie toujours "Faux" dans succés2. J'ai essayé toute les combinaison possibles, d'autre chemin pour enregistrer le fichier et de modifier les autorisations du dossier de réception mais c'est toujours le même souci.

La variable succés1 qui correspond à la ligne où j'utilise la fonction FtpSetCurrentDirectory me renvoie bien vrai et HwndConnect est à 13369500 ce qui montre que ça fonctionne bien jusque là (?)

Une idée vers quoi je dois me tourner ?

Voici le code:

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
'-------------------
'Déclaration des API
'-------------------
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 FtpGetCurrentDirectory Lib "wininet.dll" Alias "FtpGetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszCurrentDirectory As String, lpdwCurrentDirectory As Long) As Long
 
'réception d'un fichier
 
Private Sub Commande27_Click()
 
Dim HwndConnect As Long
Dim HwndOpen As Long
 
'Ouvre internet
HwndOpen = InternetOpen("SiteWeb", 0, vbNullString, vbNullString, 0)
'Connection au site ftp
HwndConnect = InternetConnect(HwndOpen, XXX, 21, XXX, XXX, 1, 0, 0)
'répertoire
succés1 = FtpSetCurrentDirectory(HwndConnect, "/")
'Téléchargement
succés2 = FtpGetFile(HwndConnect, "0006046356_valuereport_20020101010005_2101.csv", "C:/0006046356_valuereport_20020101010005_2101.csv", False, 0, 2, 0)
InternetCloseHandle HwndConnect 'Ferme la connection
InternetCloseHandle HwndOpen 'Ferme internet
 
End Sub
Merci !