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
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