Home » excel » excel – Not able to connect to FTP server using "wininet.dll" in VBA but can connect with WinSCP

excel – Not able to connect to FTP server using "wininet.dll" in VBA but can connect with WinSCP

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m using below code snippet to connect to a server using wininet.dll in VBA but not able to connect to that. But when I try to connect to the server using WinSCP 5.15.1, it’s able to connect. And when I try to use 5.5.5.0 version of WinSCP, it shows error “Couldn’t agree a key exchange algorithm”.
It also shows login error when I try to connect it using ftp command in cmd.

I’m wondering is it possible to connect to the server using wininet.dll in VBA by fixing any thing.

Public Function checkFTPpath(ByVal ServerName, ByVal Username, ByVal password, ByVal remote_path) As Boolean

  Dim hostFile As String
  Dim INet As Long
  Dim INetConn As Long
  Dim RetVal As Long
  Dim Success As Boolean

  Success = False
  RetVal = False
  INet = InternetOpen("MyFTP Control", 1&, vbNullString, vbNullString, 0&)
  If INet > 0 Then
    INetConn = InternetConnect(INet, ServerName, 21, Username, password, 1&, 0&, 0&)
      If INetConn > 0 Then
          Success = FtpSetCurrentDirectory(INetConn, "/")
          Success = FtpSetCurrentDirectory(INetConn, remote_path)
        RetVal = InternetCloseHandle(INetConn)
      End If
      RetVal = InternetCloseHandle(INet)
  End If
  checkFTPpath = Success

End Function
How to&Answers:

If you get “Couldn’t agree a key exchange algorithm” in WinSCP, it means that you are using SFTP protocol, not FTP. Wininet speaks FTP only, not SFTP.

You will have to use SFTP library. See SFTP upload in VBA.