AW: Danke Heiko - brauche noch Hilfe
13.12.2007 08:48:38
Heiko
Moin Wolfgang,
dann so:
Public Sub TestT()
Dim strUser As String, strPath As String, strhelp As String
' Dein Teil für den UserNamen
strUser = Environ("Username")
strUser = Trim$(Replace(strUser, "GST", ""))
' Zusammenbauen des Pfades aus Vorgabepfad und UserName
strPath = "C:\WINDOWS\Temp\" & strUser & "\Temp"
If Dir(strPath, vbDirectory) = "" Then
MsgBox "Das angegebene Verzeichnis existiert nicht!", vbCritical
Exit Sub
End If
Sheets("Daten").Activate
' Nun noch das TEXT; davor & Aufruf der Function die die neuste Tmp Datei als String zurückgibt. _
strhelp = FindNewestTemp(strPath)
If strhelp "" Then
strPath = "TEXT;" & FindNewestTemp(strPath)
Else
MsgBox "Es wurde keine TMP Datei im angegebenen Verzeichnis gefunden!", vbCritical
Exit Sub
End If
' Und nun nur noch importieren, fertig !!!
With ActiveSheet.QueryTables.Add(Connection:=strPath, Destination:=Range("A1"))
.Refresh
' ChDrive "C"
End With
End Sub
Public Function FindNewestTemp(strPath As String) As String
Dim strFile As String
Dim datNewestDate As Date
Dim myFileSystemObject, myFiles
If Right(strPath, 1) "\" Then strPath = strPath & "\"
Set myFileSystemObject = CreateObject("Scripting.FileSystemObject")
For Each myFiles In myFileSystemObject.GetFolder(strPath).Files
If UCase(myFileSystemObject.GetExtensionName(strPath & myFiles.Name)) = "TMP" Then
If myFiles.DateCreated > datNewestDate Then
datNewestDate = myFiles.DateCreated
strFile = myFiles.Name
End If
End If
Next myFiles
If strFile "" Then
FindNewestTemp = strPath & strFile
Else
FindNewestTemp = ""
End If
End Function
Gruß Heiko
PS: Rückmeldung wäre nett !