Bonjour,
J'essaye d'envoyer un fichier sur FTP par Macro.
Le souci a manifestement lieu du fait que la fonction InternetConnect retourne 0. En accédant au détail du message d'erreur, il semble que ce soit le code 12029 mais j'ai beau creuser, je n'arrive pas à en comprendre la signification et encore moins la cause.
Aucun souci pour déposer des fichiers sur ce serveur autrement.
Code que j'utilise ci-dessous :
FONCTIONS :
' Open the Internet object
Private Declare PtrSafe 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
' Connect to the network
Private Declare PtrSafe 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
' Get a file using FTP
Private Declare PtrSafe Function FtpGetFile Lib "Wininet.dll" Alias "FtpGetFileA" _
(ByVal hFtpSession As Long, ByVal lpszRemoteFile As String, _
ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, _
ByVal dwFlagsAndAttributes As Long, ByVal dwFlags As Long, _
ByVal dwContext As Long) As Boolean
' Send a file using FTP
Private Declare PtrSafe Function FtpPutFile Lib "Wininet.dll" Alias "FtpPutFileA" _
(ByVal hFtpSession As Long, ByVal lpszLocalFile As String, _
ByVal lpszRemoteFile As String, ByVal dwFlags As Long, _
ByVal dwContext As Long) As Boolean
' Close the Internet object
Private Declare PtrSafe Function InternetCloseHandle Lib "Wininet.dll" _
(ByVal hInet As Long) As Integer
Macro :
Sub EnvoiFichierFTP()
Dim HwndConnect As Long
Dim HwndOpen As Long, MyFile
Dim ftpAddress As String
Dim ftpUser As String
Dim ftpPassword As String
Dim ftpLocalFilepath As String
Dim ftpRemoteFilepath As String
' Définition des paramètres FTP
ftpAddress = "ftp"
ftpUser = "user"
ftpPassword = "mdp"
' Définition des emplacements du fichier local et distant
ftpLocalFilepath = "C:\Users\jerem\Desktop\Reports en cours\Macro V2.xlsm"
ftpRemoteFilepath = "./Macro V2.xlsm"
' Initialisation de la connexion FTP
HwndOpen = InternetOpen("connexionFTP", 0, vbNullString, vbNullString, 0)
' Connexion au serveur FTP
HwndConnect = InternetConnect(HwndOpen, ftpAddress, 21, ftpUser, ftpPassword, 1, 0, 0)
If HwndConnect = 0 Then
MsgBox ("Une erreur est survenue lors de la connexion. Vérifiez les informations de connexion.")
InternetCloseHandle HwndConnect
InternetCloseHandle HwndOpen
Exit Sub
End If
' Envoi du fichier
HwndPut = FtpPutFile(HwndConnect, ftpLocalFilepath, ftpRemoteFilepath, &H0, 0)
If HwndPut = 0 Then
MsgBox ("Une erreur est survenue lors de l'envoi du fichier. Vérifiez les emplacements des fichiers")
End If
' Fermeture des connexions
InternetCloseHandle HwndConnect
InternetCloseHandle HwndOpen
End Sub
Merci d'avance !
J'essaye d'envoyer un fichier sur FTP par Macro.
Le souci a manifestement lieu du fait que la fonction InternetConnect retourne 0. En accédant au détail du message d'erreur, il semble que ce soit le code 12029 mais j'ai beau creuser, je n'arrive pas à en comprendre la signification et encore moins la cause.
Aucun souci pour déposer des fichiers sur ce serveur autrement.
Code que j'utilise ci-dessous :
FONCTIONS :
' Open the Internet object
Private Declare PtrSafe 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
' Connect to the network
Private Declare PtrSafe 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
' Get a file using FTP
Private Declare PtrSafe Function FtpGetFile Lib "Wininet.dll" Alias "FtpGetFileA" _
(ByVal hFtpSession As Long, ByVal lpszRemoteFile As String, _
ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, _
ByVal dwFlagsAndAttributes As Long, ByVal dwFlags As Long, _
ByVal dwContext As Long) As Boolean
' Send a file using FTP
Private Declare PtrSafe Function FtpPutFile Lib "Wininet.dll" Alias "FtpPutFileA" _
(ByVal hFtpSession As Long, ByVal lpszLocalFile As String, _
ByVal lpszRemoteFile As String, ByVal dwFlags As Long, _
ByVal dwContext As Long) As Boolean
' Close the Internet object
Private Declare PtrSafe Function InternetCloseHandle Lib "Wininet.dll" _
(ByVal hInet As Long) As Integer
Macro :
Sub EnvoiFichierFTP()
Dim HwndConnect As Long
Dim HwndOpen As Long, MyFile
Dim ftpAddress As String
Dim ftpUser As String
Dim ftpPassword As String
Dim ftpLocalFilepath As String
Dim ftpRemoteFilepath As String
' Définition des paramètres FTP
ftpAddress = "ftp"
ftpUser = "user"
ftpPassword = "mdp"
' Définition des emplacements du fichier local et distant
ftpLocalFilepath = "C:\Users\jerem\Desktop\Reports en cours\Macro V2.xlsm"
ftpRemoteFilepath = "./Macro V2.xlsm"
' Initialisation de la connexion FTP
HwndOpen = InternetOpen("connexionFTP", 0, vbNullString, vbNullString, 0)
' Connexion au serveur FTP
HwndConnect = InternetConnect(HwndOpen, ftpAddress, 21, ftpUser, ftpPassword, 1, 0, 0)
If HwndConnect = 0 Then
MsgBox ("Une erreur est survenue lors de la connexion. Vérifiez les informations de connexion.")
InternetCloseHandle HwndConnect
InternetCloseHandle HwndOpen
Exit Sub
End If
' Envoi du fichier
HwndPut = FtpPutFile(HwndConnect, ftpLocalFilepath, ftpRemoteFilepath, &H0, 0)
If HwndPut = 0 Then
MsgBox ("Une erreur est survenue lors de l'envoi du fichier. Vérifiez les emplacements des fichiers")
End If
' Fermeture des connexions
InternetCloseHandle HwndConnect
InternetCloseHandle HwndOpen
End Sub
Merci d'avance !