Witam
Znalazłem kod, który łączy się z serwerem FTP i ściąga nazwy plików z folderu. Wszystko działa, ale nie wiem dlaczego - ucina 2 pierwsze litery
plik : "Przykładowa nazwa.xls" jest wyświetlany jako "zykładowa nazwa.xls".
Dodam, że działam na Excelu 64bit i być może tu jest problem, bo musiałem trochę przerobić deklaracje funkcji, żeby w ogóle to działało.
Prośba o podpowiedź, gdzie w ogóle szukać problemu.
Dziękuję
Kod:
Option Explicit
Private Type WIN32_FIND_DATA_W
dwFileAttributes As Long
ftCreationTime As Currency
ftLastAccessTime As Currency
ftLastWriteTime As Currency
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName(520) As Byte
cAlternateFileName(28) As Byte
End Type
Private Declare PtrSafe Function InternetOpenA Lib "wininet.dll" ( _
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 InternetConnectA Lib "wininet.dll" ( _
ByVal hInternetSession As Long, _
ByVal sServerName As String, _
ByVal nServerPort As Long, _
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 FtpFindFirstFileW Lib "wininet.dll" ( _
ByVal hConnect As LongPtr, _
ByVal lpszSearchFile As LongPtr, _
ByVal lpFindFileData As LongPtr, _
ByVal dwFlags As LongPtr, _
ByVal dwContext As LongPtr) As Long
Private Declare PtrSafe Function InternetFindNextFileW Lib "wininet.dll" ( _
ByVal hConnect As Long, _
ByVal lpvFindData As LongPtr) As Long
Private Declare PtrSafe Function InternetCloseHandle Lib "wininet.dll" ( _
ByVal hInet As Long) As Long
Private Declare PtrSafe Sub RtlZeroMemory Lib "kernel32" ( _
dst As Any, _
ByVal nBytes As Long)
Private Function remove_bittynull(ByVal strData As String) As String
' helper function remove leading null chars
remove_bittynull = Left$(strData, InStr(1, strData, vbNullChar) - 1)
End Function
Function LastRow() As Long
If Sheets(1).Range("A1").Value = "" Then
LastRow = 1
ElseIf Sheets(1).Range("A1").Value <> "" And Sheets(1).Range("A2").Value = "" Then
LastRow = 2
Else
LastRow = Sheets(1).Range("A1").End(xlDown).Row + 1
End If
End Function
Public Sub EnumFtpDirectory(ByVal strHost As String, ByVal lngPort As Long, ByVal strUser As String, ByVal strPass As String, ByVal strSetDir As String)
' Returns: List of files from a ftp directory(Wide Version)
' Usage: Call EnumFtpDirectory("ftp.server.com", 21, "user", "pass", "/TEST/", List1)
Dim hOpen As Long
Dim hConn As Long
Dim hFind As Long
Dim w32 As WIN32_FIND_DATA_W
Dim licznik As Integer
licznik = 0
' // initialize struct (zero byte)
RtlZeroMemory w32, Len(w32)
' // initialize ftp sessions
hOpen = InternetOpenA("ftpdir", 1, vbNullString, vbNullString, 1)
' // initialize credentials
hConn = InternetConnectA(hOpen, strHost, lngPort, strUser, strPass, 1, 0, 2)
' // initialize enumeration
hFind = FtpFindFirstFileW(hConn, StrPtr(strSetDir), VarPtr(w32), 0, 0)
' // find all files and add to listbox (me.listbox1)
Do
Sheets(1).Cells(LastRow, 1) = remove_bittynull(w32.cFileName)
licznik = licznik + 1
If licznik = 2 Then
Exit Sub
End If
Loop Until InternetFindNextFileW(hFind, VarPtr(w32)) = 0
' // clean up handles
InternetCloseHandle hConn
InternetCloseHandle hOpen
InternetCloseHandle hFind
End Sub