I'm currently working on a VBA project where I need to check if a file exists in an FTP directory using XMLHTTP. However, I'm facing an issue as I always receive a response value of 0.
Dim ftpServer As String
Dim ftpUsername As String
Dim ftpPassword As String
Dim filePath As String
Dim ftpURL As String
Dim httpRequest As Object
Dim ftpResponse As String
' FTP server details
ftpServer = "37.***.144.232"
ftpUsername = "***usr***"
ftpPassword = "*****"
' File path and name on the FTP server
filePath = "/a.jpg"
' Construct the FTP URL
ftpURL = "ftp://" & ftpServer & filePath
MsgBox ftpURL
' Create HTTP request object
Set httpRequest = CreateObject("Microsoft.XMLHTTP")
' Open FTP URL using basic authentication
httpRequest.Open "GET", ftpURL, False, ftpUsername, ftpPassword
' Send the request
httpRequest.Send
' Get the FTP response status
ftpResponse = httpRequest.Status
' Check the response
If ftpResponse = "200" Then
' Connection to the server successful
MsgBox "Connected to server successfully!"
' Check if file exists
If Len(httpRequest.responseText) > 0 Then
' File exists
MsgBox "File exists!"
Else
' File doesn't exist
MsgBox "File does not exist!"
End If
ElseIf ftpResponse = "404" Then
' File doesn't exist
MsgBox "File does not exist!"
Else
' Other response status or error occurred
MsgBox "An error occurred. Response status: " & ftpResponse
End If
I have already verified that the FTP directory path and the file name are correct. I suspect there might be an issue with my implementation of the XMLHTTP request.
I would appreciate any assistance in identifying the problem and finding a solution. Additionally, if there is an alternative approach or a better method to achieve this file existence check in VBA, please let me know.