Ich suche nach VBA Code, der mir auf einem Remote Server überprüft, ob eine bestimmte Datei vorhanden ist.
Mittels FTP kann ich mittels FTP auf die Datei zugriefen, doch nur, wenn diese Datei vorhanden ist.
Danke im Voraus.
Grüße
Peter
If Dir("\\Hostname\Pfad\Datei") = "" Then
MsgBox "Datei nicht vorhanden"
End If
Hilfts?Option Explicit
' 2001 by Thomas Rodemer
Private Declare 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 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 Function InternetCloseHandle Lib "wininet.dll" _
(ByVal hInet As Long) As Integer
Private Declare Function FtpSetCurrentDirectory Lib "wininet.dll" _
Alias "FtpSetCurrentDirectoryA" (ByVal hFtpSession As _
Long, ByVal lpszDirectory As String) As Long
Private Declare 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 Function InternetFindNextFile Lib "wininet.dll" _
Alias "InternetFindNextFileA" (ByVal hFind As Long, _
lpvFindData As WIN32_FIND_DATA) As Long
Private Declare 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 Long
Private Declare 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 Long
Private Declare Function FtpDeleteFile Lib "wininet.dll" _
Alias "FtpDeleteFileA" (ByVal hFtpSession As Long, _
ByVal lpszFileName As String) As Long
Private Declare Function FtpRenameFile Lib "wininet.dll" _
Alias "FtpRenameFileA" (ByVal hFtpSession As Long, _
ByVal lpszFromFileName As String, ByVal lpszToFileName _
As String) As Long
Private Declare Function FtpCreateDirectory Lib "wininet" _
Alias "FtpCreateDirectoryA" (ByVal hFtpSession As _
Long, ByVal lpszDirectory As String) As Long
Private Declare Function FtpRemoveDirectory Lib "wininet" _
Alias "FtpRemoveDirectoryA" (ByVal hFtpSession As _
Long, ByVal lpszDirectory As String) As Long
Private Declare Function InternetGetLastResponseInfo Lib _
"wininet.dll" Alias "InternetGetLastResponseInfoA" _
(lpdwError As Long, ByVal lpszBuffer As String, _
lpdwBufferLength As Long) As Long
Const ERROR_NO_MORE_FILES = 18
Const ERROR_INTERNET_EXTENDED_ERROR = 12003
Const FTP_TRANSFER_TYPE_BINARY = &H0
Const FTP_TRANSFER_TYPE_ASCII = &H1
Const INTERNET_FLAG_PASSIVE = &H8000000
Const INTERNET_FLAG_RELOAD = &H80000000
Const INTERNET_FLAG_KEEP_CONNECTION = &H400000
Const INTERNET_FLAG_MULTIPART = &H200000
Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Const INTERNET_OPEN_TYPE_DIRECT = 1
Const INTERNET_OPEN_TYPE_PROXY = 3
Const INTERNET_INVALID_PORT_NUMBER = 0
Const INTERNET_SERVICE_FTP = 1
Const INTERNET_SERVICE_GOPHER = 2
Const INTERNET_SERVICE_HTTP = 3
Private Declare Function FileTimeToSystemTime Lib "kernel32" _
(lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) _
As Long
Const MAX_PATH = 260
Const NO_ERROR = 0
Const FILE_ATTRIBUTE_READONLY = &H1
Const FILE_ATTRIBUTE_HIDDEN = &H2
Const FILE_ATTRIBUTE_SYSTEM = &H4
Const FILE_ATTRIBUTE_DIRECTORY = &H10
Const FILE_ATTRIBUTE_ARCHIVE = &H20
Const FILE_ATTRIBUTE_NORMAL = &H80
Const FILE_ATTRIBUTE_TEMPORARY = &H100
Const FILE_ATTRIBUTE_COMPRESSED = &H800
Const FILE_ATTRIBUTE_OFFLINE = &H1000
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
Dim Verbundenoderwas As Boolean
Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
Private Sub Command1_Click()
Dim nFlag As Long, hOpen As Long, hConnection As Long
Dim Result&
Dim strPath$, strDateiName$
Dim strUsername$, strPasswort$, strServer$, strSubDir$
strServer = "ftp.deinserver.de" 'Dein Server
strUsername$ = "UserName" 'Dein User Name
strPasswort = "Passwort" 'Dein Passwort
strSubDir$ = "UnterOrdner" 'Unterordner
'Pfad für Dateien?
strPath$ = IIf(Right$(ThisWorkbook.Path, 1) = "\", ThisWorkbook.Path, ThisWorkbook.Path & "\")
'Datei Name?
strDateiName$ = "Meine Datei.xls"
hOpen = InternetOpen("FTP", 1, vbNullString, vbNullString, 0)
If hOpen = 0 Then
MsgBox ("Es konnte keine Verbindung hergestellt werden!")
Exit Sub
End If
'Hier alle Angaben zum User deinen Bedürfnissen anpassen
hConnection = InternetConnect(hOpen, strServer, 0, strUsername, strPasswort, 1, nFlag, 0)
'Eventuelles wechseln in ein Subdir:
Result = FtpSetCurrentDirectory(hConnection, strSubDir$)
'Download einer File:
Result = FtpGetFile(hConnection, "Dateiname.xls", strPath$ & strDateiName$, False, &H0, &H0, 0)
'Upload einer File:
Result = FtpPutFile(hConnection, strPath$ & strDateiName$, strDateiName$, &H0, 0)
'Trennen der Verbindung:
If hConnection <> 0 Then InternetCloseHandle (hConnection)
If hOpen <> 0 Then InternetCloseHandle (hOpen)
End Sub
Gruß Tino