ftpgetfile giving unreadable content

owardstc

New Member
Joined
Oct 4, 2017
Messages
5
Hi,
i have macro to copy excel file from ftp server and save it in local folder and it's working fine but when I try to open the file i get error "Excel found unreadable content" , the file in the server working but when I copy it giving this message :(

this is my code

Code:
Const MAX_PATH = 260

Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type

Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type

 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

 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

 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

 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
        
Private Declare PtrSafe Function FtpSetCurrentDirectory Lib "wininet.dll" Alias _
 "FtpSetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) _
 As Boolean
 
Private Declare PtrSafe Function FtpFindFirstFile Lib "wininet.dll" Alias "FtpFindFirstFileA" _
    (ByVal hFtpSession As Long, ByVal lpszSearchFile As String, _
    lpFindFileData As WIN32_FIND_DATA, ByVal dwFlags As Long, _
    ByVal dwContent As Long) As Long


 Private Declare PtrSafe Function InternetCloseHandle _
   Lib "wininet.dll" _
     (ByVal hInet As Long) As Integer
     

Sub DownloadTransformUploadFTP()

Dim lngINetConn
Dim lngINet
Dim blnRC As Boolean
Dim username As String
Dim password As String
Dim serverName As String
Const ASCII_TRANSFER = 1
Const BINARY_TRANSFER = 2
Dim fData As WIN32_FIND_DATA
Dim Check1
Dim Check2

    serverName = "172.20.198.10"
    username = "os057481"
    password = "Oward_06"
    localFile = "C:\Users\Ericsson\Desktop\STC\Auto_Run\LTE_DATA\LTE_Data.xlsx"
    hostFile = "/home/ah034343/AREA/LTE_Data.xlsx"

blnRC = False

'InternetOpen: Initializes an application's use of the WinINet functions
'Returns a valid handle that the application passes to subsequent WinINet functions.
'If InternetOpen fails, it returns NULL. To retrieve a specific error message, call GetLastError.
lngINet = InternetOpen("MyFTP Control", 1&, vbNullString, vbNullString, 0&) '
If lngINet > 0 Then

    'InternetConnect:Opens an File Transfer Protocol (FTP) or HTTP session for a given site.
    'Returns a valid handle to the session if the connection is successful, or NULL otherwise. To retrieve extended error information,
    'call GetLastError. An application can also use InternetGetLastResponseInfo to determine why access to the service was denied.
    
    lngINetConn = InternetConnect(lngINet, serverName, 0, username, password, 1, 0, 0)
    If lngINetConn > 0 Then
        
        'FtpSetCurrentDirectory: Changes to a different working directory on the FTP server.
        'Check1 = FtpSetCurrentDirectory(lngINetConn, "/outgoing" + vbNullString)
        Check1 = FtpSetCurrentDirectory(lngINetConn, "/home/ah034343/AREA/")
        fData.cFileName = String(MAX_PATH, 0)

        Check2 = FtpFindFirstFile(lngINetConn, "LTE_Data.xlsx", fData, 0, 0)
        
        'FtpGetFile : Retrieves a file from the FTP server and stores it under the specified file name,
        'creating a new local file in the process.
        
        'blnRC = FtpGetFile(lngINetConn, "/outgoing/" & fData.cFileName, localFile, 0, 0, 1, 0)
        'blnRC = FtpGetFile(lngINetConn, hostFile, localFile, 0, 0, 1, 0)
        blnRC = FtpGetFile(lngINetConn, hostFile, localFile, False, FILE_ATTRIBUTE_NORMAL, INTERNET_FLAG_RELOAD, &H0)
        InternetCloseHandle lngINetConn
    End If
    InternetCloseHandle lngINet
End If

End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
You create a BINARY_TRANSFER constant, but you never actually use it as far as I can see.
The file you get is most likely copied using ASCII, thus the error.
Just a guess though.
 
Upvote 0
You need to make the ftp session binary somehow.

hi, finally i solve it

i change this line

Code:
blnRC = FtpGetFile(lngINetConn, hostFile, localFile, False, FILE_ATTRIBUTE_NORMAL, INTERNET_FLAG_RELOAD, &H0)

to be like this

Code:
blnRC = FtpGetFile(lngINetConn, hostFile, localFile, False, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_BINARY, 0)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top