erreur sur nom de fichier

PHILIP

XLDnaute Occasionnel
Bonsoir à tous ,

JE reviens vers vous car je n'ai toujours pas reglé mon probleme de macro
Je voudrais importer automatiquement 2 fichiers txt par jours d'un serveur FTP.
Pour ce faire j'utilise monfichier ="mon*.txt" car si le fichier change chaque fois sa numérotation, les trois 1eres letrres soit "mon" jamais Mais ma macro me signale qu"elle ne peut importer le fichier "mon*.txt"!! Ai-je fais une erreur dans l'ecriture? car si j'utilise le nom exact du fichier soit
"mon002101112161809.txt" il me l'importe bien ? pourriez vous m'aider ?
Voici le code qui peut être essayé, le fichier se trouvant encore sur le serveur
Code:
Option Explicit
'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()
'On Error GoTo Err_Sub
    Dim HwndConnect As Long
  Dim HwndOpen As Long, MyFile
  ' Lance l'ouverture du site ftp
  HwndOpen = InternetOpen("ftp://pundo100f@212.184.120.18", 0, vbNullString, vbNullString, 0)
  'Connection au site ftp
  HwndConnect = InternetConnect(HwndOpen, "212.184.120.18", 21, "gst100", "pundo100f", 1, 0, 0)
  'positionnement du curseur dans le répertoire
  FtpSetCurrentDirectory HwndConnect, "/travail"
   'Téléchargement du fichier
  Dim Rep, NomFichier As String, VPathFileName As String
  NomFichier = "mon002101112161809.txt"
  VPathFileName = "T:\STATUS\Statuts 2010\Benelux.txt"
  Rep = FtpGetFile(HwndConnect, NomFichier, 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 : " & ("mon*.txt") & vbCrLf _
      & "Arrêt de la macro !"
    End
  End If
  ' Ferme le handle de connection puis celui d'Internet
  InternetCloseHandle HwndConnect
  InternetCloseHandle HwndOpen
  
  Exit Sub
  End
End Sub
 
Dernière édition:
C

Compte Supprimé 979

Guest
Re : erreur sur nom de fichier

Bonjour Philip,

Tu ne peux pas utiliser "*" dans ton nom de fichier pour l'importer ça ne fonctionnera jamais !

Pour récupérer les 2 derniers noms de tes fichiers, je ne sais pas comment tu pourrais faire :confused:

PS : merci de ne pas laisser ton login et mdp d'accès au FTP

a+
 

Discussions similaires

Statistiques des forums

Discussions
312 228
Messages
2 086 421
Membres
103 205
dernier inscrit
zch