charger fichier excel sur serveur FTP en VBA

simonlagaffe

XLDnaute Nouveau
Bonjour a tous,

Je recherche le moyen de pouvoir charger un fichier excel sur un serveur FTP en utilisant le VBA... mais jusqu'à présent sans succés!
Quelqu'un pourrait il m'aider?!
Merci pour votre aide
 
C

Compte Supprimé 979

Guest
Re : charger fichier excel sur serveur FTP en VBA

Salut SimonLaGaffe,

Voilà le code que j'utilise pour importer un fichier d'un serveur FTP
Code:
Option Explicit
Public Login As String, Mdp As String, NumFic As String
'Déclaration des API Windows pour permettre la récupération du fichier sur le serveur FTP
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
 
 
Public Sub ImportFTP(FicELO As string)
On Error GoTo Err_Sub
  ' Marco développé par : Shwin - [URL="http://www.developpez.net/forums/member.php?u=26564"]Forum des développeurs - Voir le profil: shwin[/URL]
  ' Source : [URL="http://access.developpez.com/sources/?page=Fichiers#EnvoiFTP"]Access - Les Sources - Club d'entraide des développeurs francophones[/URL]
  Dim HwndConnect As Long
  Dim HwndOpen As Long, MyFile
  ' On récupère l'adresse IP dans les CGR
  Dim SiteFTP
  ' Définir l'adresse IP - ICI
  SiteFTP = "0.0.0.0"  ' Lance l'ouverture du site ftp
  HwndOpen = InternetOpen("ftp://" & SiteFTP & "/travail", 0, vbNullString, vbNullString, 0)
  'Connection au site ftp
  HwndConnect = InternetConnect(HwndOpen, SiteFTP, 21, Login, Mdp, 1, 0, 0)
  'positionnement du curseur dans le répertoire
  FtpSetCurrentDirectory HwndConnect, "/travail"
  ' Téléchargement du fichier ELO  
  Dim Rep
  Rep = FtpGetFile(HwndConnect, FicELO, VPathFileName, False, 0, &H0, 0)
  ' Vérifier que le fichier a bien été téléchargé
  If Rep = False Then
    InternetCloseHandle HwndConnect 'Ferme la connection
    InternetCloseHandle HwndOpen 'Ferme internet
    MsgBox "Impossible d'importer le fichier : " & FicELO & vbCrLf _
      & "Arrêt de la macro !"
    End
  End If
  ' Ferme le handle de connection puis celui d'Internet
  InternetCloseHandle HwndConnect
  InternetCloseHandle HwndOpen
 
  Exit Sub
 
Err_Sub:
  MsgBox "Une erreur c'est produite, arrêt de la macro"
  InternetCloseHandle HwndConnect 'Ferme la connection
  InternetCloseHandle HwndOpen 'Ferme internet
  FlgErr = True
  End
End Sub

Il te faut utiliser la fonction : FtpPutFile

Voila, A+
 

Discussions similaires

Réponses
6
Affichages
336

Statistiques des forums

Discussions
312 320
Messages
2 087 218
Membres
103 497
dernier inscrit
JP9231