Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1516to1520
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Inhaltsverzeichnis

Problem Ordner einlesen Win8/ Excel 2013

Problem Ordner einlesen Win8/ Excel 2013
28.09.2016 09:51:42
Volker
Hallo Gemeinde,
ich bin mal wieder an einem Punkt, an dem ich eure Hilfe brauche.
Folgender Sachverhalt:
Ich lese Dateien aus einem Ordner aus.
Test 1: Windows 7 64 bit mit Excel 2010 64 bit / Das funktioniert!
Test 2: Windows 7 64 bit mit Excel 2013 64 bit / Das funktioniert!
Test 3 :Windows 8 64 bit mit Excel 2013 64 bit / Das funktioniert NICHT!
Ich habe 3 Bilder angehängt.
Bild 1 Zeit das Auslesemakro. An der Stelle " Loop While FindNextFile(hFile, FD)"
hängt sich Excel dann auf und startet neu.
Bild 2 zeigt alle Variablen von Test 2 an besagter Stelle vor dem Loop.
Bild 3 zeigt alle Variablen von Test 3 (Abbruch) an besagter Stelle vor dem Loop.
Kann mir da jemand weiter helfen?
Ich hoffe, alle nötigen Infos gegeben zu haben.
Das wäre echt klasse!
Gruß Volker
Userbild
Userbild
Userbild

10
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Problem Ordner einlesen Win8/ Excel 2013
28.09.2016 11:45:47
Nepumuk
Hallo,
lass mal die Deklarationen der API's sehen.
Gruß
Nepumuk
AW: Problem Ordner einlesen Win8/ Excel 2013
29.09.2016 13:05:49
Volker
Hallo Nepumuk,
sorry für die späte Antwort.
Hier die Deklaration:
Userbild
AW: Problem Ordner einlesen Win8/ Excel 2013
29.09.2016 14:05:42
Nepumuk
Hallo,
kannst du bitte der Code und kein Bild vom Code einstellen? Ich hab nämlich keine Lust abzuschreiben.
Was ich schon sagen kann, etliche Deklarationen sind fehlerhaft. Es genügt nicht einfach nur PtrSafe einzufügen, du musst auch die Datentypen der Parameter und die Rückgabewerte anpassen.
Gruß
Nepumuk
AW: Problem Ordner einlesen Win8/ Excel 2013
29.09.2016 14:18:58
Volker
Aha.
Hier ist der Text.
Gruß Volker
Option Explicit
Public Declare PtrSafe Function GetLogicalDrives _
Lib "kernel32" () As Long
Public Declare PtrSafe Function GetDriveType _
Lib "kernel32" Alias "GetDriveTypeA" _
(ByVal lpRootPathName As String) As Integer
Public Declare PtrSafe Function GetLogicalDriveStrings _
Lib "kernel32" Alias "GetLogicalDriveStringsA" _
(ByVal nBufferLength As Long, _
ByVal lpBuffer As String) As Long
Public Declare PtrSafe Function FindFirstFile Lib "kernel32" _
Alias "FindFirstFileA" (ByVal lpFileName As String, _
lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare PtrSafe Function FindNextFile Lib "kernel32" _
Alias "FindNextFileA" (ByVal hFindFile As Long, _
lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare PtrSafe Function FindClose Lib "kernel32" (ByVal _
hFindFile As Long) As Long
Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Const MAX_PATH = 259
Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nxFilesizeHigh As Long
nxFilesizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
Const FILE_ATTRIBUTE_ARCHIVE = &H20
Const FILE_ATTRIBUTE_COMPRESSED = &H800
Const FILE_ATTRIBUTE_DIRECTORY = &H10
Const FILE_ATTRIBUTE_HIDDEN = &H2
Const FILE_ATTRIBUTE_NORMAL = &H80
Const FILE_ATTRIBUTE_READONLY = &H1
Const FILE_ATTRIBUTE_SYSTEM = &H4
Const FILE_ATTRIBUTE_TEMPORARY = &H100
Anzeige
AW: Problem Ordner einlesen Win8/ Excel 2013
29.09.2016 14:53:43
Nepumuk
Hallo,
die fehlerhaften Deklarationen:
Private Declare PtrSafe Function FindFirstFile Lib "kernel32.dll" Alias "FindFirstFileA" ( _
    ByVal lpFileName As String, _
    ByRef pFindFileData As WIN32_FIND_DATA) As LongPtr
Private Declare PtrSafe Function FindNextFile Lib "kernel32.dll" Alias "FindNextFileA" ( _
    ByVal hFindFile As LongPtr, _
    ByRef lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare PtrSafe Function FindClose Lib "kernel32.dll" ( _
    ByVal hFindFile As LongPtr) As Long

Du musst nun im Code die entsprechenden Variablen anpassen.
Gruß
Nepumuk
Anzeige
AW: Problem Ordner einlesen Win8/ Excel 2013
29.09.2016 15:05:37
Volker
Besten Dank Nepumuk, das werde ich heute Abend gleich mal versuchen.
Ich glaube, ich sollte das VBA Gut wohl eher in VBA bescheiden umwandeln....
Ich gebe schnellstmöglich ein Feedback!
AW: Problem Ordner einlesen Win8/ Excel 2013
29.09.2016 17:23:31
Volker
Oh man.
Der kleine Text "Variablen anpassen" zeigt mir, dass ich mich in diesen Dingen so überhaupt nicht aus kenne. Ich sollte mich wohl mit dem Thema Variablen etwas näher befassen. :-(
Vielleicht kannst du mir da noch nen Tipp geben, Nepumuk?
Hier sind die 2 Makros. Beim ersten FindFirstFile... kommt auch gleich die Fehlermeldung!
Besten Dank schon mal!!!!
Sub GetAllFiles(ByVal Root$, ByVal strPath$, ByRef Field$(), ByRef lngFileAttributes&(), ByVal  _
strSearchFile$, ByVal strInstanz&)
Dim File$, hFile&, FD As WIN32_FIND_DATA
Dim SFile$, ShFile&, SFD As WIN32_FIND_DATA
Dim xAttrib&
Dim SRoot$
strInstanz = strInstanz + 1
If Right(Root, 1)  "\" Then Root = Root & "\"
If strInstanz = 1 Then
SRoot = Root
ShFile = FindFirstFile(SRoot & strSearchFile$, SFD)
If ShFile > 0 Then
Do
SFile = Left(SFD.cFileName, InStr(SFD.cFileName, Chr(0)) - 1)
If Not (SFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY Then
If (SFile  ".") And (SFile  "..") Then
Field(UBound(Field)) = SRoot & SFile
lngFileAttributes(UBound(Field)) = SFD.dwFileAttributes
ReDim Preserve Field(0 To UBound(Field) + 1)
ReDim Preserve lngFileAttributes&(0 To UBound(Field))
End If
End If
Loop While FindNextFile(ShFile, SFD)
End If
End If
hFile = FindFirstFile(Root & strPath, FD)
If hFile = 0 Then Exit Sub
Do
File = Left(FD.cFileName, InStr(FD.cFileName, Chr(0)) - 1)
xAttrib& = FD.dwFileAttributes
If (FD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY Then
If (File  ".") And (File  "..") Then
SFile = File
SRoot = Root
GetAllFiles Root & File, strPath, Field, lngFileAttributes, strSearchFile$, (strInstanz)
If Right(SFile, 1)  "\" Then SFile = SFile & "\"
SRoot = SRoot & SFile
ShFile = FindFirstFile(SRoot & strSearchFile$, SFD)
If ShFile > 0 Then
Do
SFile = Left(SFD.cFileName, InStr(SFD.cFileName, Chr(0)) - 1)
If Not (SFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY  _
Then
If (SFile  ".") And (SFile  "..") Then
Field(UBound(Field)) = SRoot & SFile
lngFileAttributes(UBound(Field)) = SFD.dwFileAttributes
ReDim Preserve Field(0 To UBound(Field) + 1)
ReDim Preserve lngFileAttributes&(0 To UBound(Field))
End If
End If
Loop While FindNextFile(ShFile, SFD)
End If
End If
Call FindClose(ShFile)
End If
Stop
Loop While FindNextFile(hFile, FD)
Call FindClose(hFile)
End Sub

Sub GetAllDirctory(ByVal Root$, ByVal strPath$, ByRef Field$(), ByRef lngFileAttributes&(),  _
ByVal strSearchFile$, ByVal strInstanz&)
Dim File$, hFile&, FD As WIN32_FIND_DATA
Dim SFile$, ShFile&, SFD As WIN32_FIND_DATA
Dim xAttrib&
Dim SRoot$
strInstanz = strInstanz + 1
If Right(Root, 1)  "\" Then Root = Root & "\"
If strInstanz = 1 Then
SRoot = Root
ShFile = FindFirstFile(SRoot & strSearchFile$, SFD)
If ShFile > 0 Then
Do
SFile = Left(SFD.cFileName, InStr(SFD.cFileName, Chr(0)) - 1)
If (SFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY Then
If (SFile  ".") And (SFile  "..") Then
Field(UBound(Field)) = SRoot & SFile
lngFileAttributes(UBound(Field)) = SFD.dwFileAttributes
ReDim Preserve Field(0 To UBound(Field) + 1)
ReDim Preserve lngFileAttributes&(0 To UBound(Field))
End If
End If
Loop While FindNextFile(ShFile, SFD)
End If
End If
hFile = FindFirstFile(Root & strPath, FD)
If hFile = 0 Then Exit Sub
Do
File = Left(FD.cFileName, InStr(FD.cFileName, Chr(0)) - 1)
xAttrib& = FD.dwFileAttributes
If (FD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY Then
If (File  ".") And (File  "..") Then
SFile = File
SRoot = Root
GetAllDirctory Root & File, strPath, Field, lngFileAttributes, strSearchFile$, (strInstanz)
If Right(SFile, 1)  "\" Then SFile = SFile & "\"
SRoot = SRoot & SFile
ShFile = FindFirstFile(SRoot & strSearchFile$, SFD)
If ShFile > 0 Then
Do
SFile = Left(SFD.cFileName, InStr(SFD.cFileName, Chr(0)) - 1)
If (SFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY Then
If (SFile  ".") And (SFile  "..") Then
Field(UBound(Field)) = SRoot & SFile
lngFileAttributes(UBound(Field)) = SFD.dwFileAttributes
ReDim Preserve Field(0 To UBound(Field) + 1)
ReDim Preserve lngFileAttributes&(0 To UBound(Field))
End If
End If
Loop While FindNextFile(ShFile, SFD)
End If
End If
Call FindClose(ShFile)
End If
Loop While FindNextFile(hFile, FD)
Call FindClose(hFile)
End Sub

Anzeige
AW: Problem Ordner einlesen Win8/ Excel 2013
29.09.2016 17:51:01
Nepumuk
Hallo,
ich hab es mal so grob überarbeitet. Teste mal:
Option Explicit

Private Declare PtrSafe Function FindFirstFile Lib "kernel32.dll" Alias "FindFirstFileA" ( _
    ByVal lpFileName As String, _
    ByRef pFindFileData As WIN32_FIND_DATA) As LongPtr
Private Declare PtrSafe Function FindNextFile Lib "kernel32.dll" Alias "FindNextFileA" ( _
    ByVal hFindFile As LongPtr, _
    ByRef lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare PtrSafe Function FindClose Lib "kernel32.dll" ( _
    ByVal hFindFile As LongPtr) As Long

Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type

Const MAX_PATH = 259

Type WIN32_FIND_DATA
    dwFileAttributes As Long
    ftCreationTime As FILETIME
    ftLastAccessTime As FILETIME
    ftLastWriteTime As FILETIME
    nxFilesizeHigh As Long
    nxFilesizeLow As Long
    dwReserved0 As Long
    dwReserved1 As Long
    cFileName As String * MAX_PATH
    cAlternate As String * 14
End Type

Const FILE_ATTRIBUTE_ARCHIVE = &H20
Const FILE_ATTRIBUTE_COMPRESSED = &H800
Const FILE_ATTRIBUTE_DIRECTORY = &H10
Const FILE_ATTRIBUTE_HIDDEN = &H2
Const FILE_ATTRIBUTE_NORMAL = &H80
Const FILE_ATTRIBUTE_READONLY = &H1
Const FILE_ATTRIBUTE_SYSTEM = &H4
Const FILE_ATTRIBUTE_TEMPORARY = &H100

Sub GetAllFiles(ByVal Root$, ByVal strPath$, ByRef Field$(), ByRef lngFileAttributes&(), ByVal _
        strSearchFile$, ByVal strInstanz&)

    
    Dim File$, hFile As LongPtr, FD As WIN32_FIND_DATA
    Dim SFile$, ShFile As LongPtr, SFD As WIN32_FIND_DATA
    Dim xAttrib&
    Dim SRoot$
    strInstanz = strInstanz + 1
    If Right$(Root, 1) <> "\" Then Root = Root & "\"
    
    If strInstanz = 1 Then
        SRoot = Root
        ShFile = FindFirstFile(SRoot & strSearchFile$, SFD)
        If ShFile > 0 Then
            Do
                SFile = Left$(SFD.cFileName, InStr(SFD.cFileName, Chr(0)) - 1)
                If Not (SFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY Then
                    If (SFile <> ".") And (SFile <> "..") Then
                        Field(UBound(Field)) = SRoot & SFile
                        lngFileAttributes(UBound(Field)) = SFD.dwFileAttributes
                        Redim Preserve Field(0 To UBound(Field) + 1)
                        Redim Preserve lngFileAttributes&(0 To UBound(Field))
                    End If
                End If
            Loop While FindNextFile(ShFile, SFD)
            Call FindClose(ShFile)
        End If
    End If
    
    hFile = FindFirstFile(Root & strPath, FD)
    If hFile = 0 Then Exit Sub
    Do
        File = Left$(FD.cFileName, InStr(FD.cFileName, Chr(0)) - 1)
        xAttrib& = FD.dwFileAttributes
        If (FD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY Then
            If (File <> ".") And (File <> "..") Then
                SFile = File
                SRoot = Root
                GetAllFiles Root & File, strPath, Field, lngFileAttributes, strSearchFile$, (strInstanz)
                If Right$(SFile, 1) <> "\" Then SFile = SFile & "\"
                SRoot = SRoot & SFile
                ShFile = FindFirstFile(SRoot & strSearchFile$, SFD)
                If ShFile > 0 Then
                    Do
                        SFile = Left$(SFD.cFileName, InStr(SFD.cFileName, Chr(0)) - 1)
                        If Not (SFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY Then
                            If (SFile <> ".") And (SFile <> "..") Then
                                Field(UBound(Field)) = SRoot & SFile
                                lngFileAttributes(UBound(Field)) = SFD.dwFileAttributes
                                Redim Preserve Field(0 To UBound(Field) + 1)
                                Redim Preserve lngFileAttributes&(0 To UBound(Field))
                            End If
                        End If
                    Loop While FindNextFile(ShFile, SFD)
                End If
            End If
            Call FindClose(ShFile)
        End If
    Loop While FindNextFile(hFile, FD)
    Call FindClose(hFile)
End Sub

Sub GetAllDirctory(ByVal Root$, ByVal strPath$, ByRef Field$(), ByRef lngFileAttributes&(), _
        ByVal strSearchFile$, ByVal strInstanz&)

    
    Dim File$, hFile As LongPtr, FD As WIN32_FIND_DATA
    Dim SFile$, ShFile As LongPtr, SFD As WIN32_FIND_DATA
    Dim xAttrib&
    Dim SRoot$
    strInstanz = strInstanz + 1
    If Right$(Root, 1) <> "\" Then Root = Root & "\"
    
    If strInstanz = 1 Then
        SRoot = Root
        ShFile = FindFirstFile(SRoot & strSearchFile$, SFD)
        If ShFile > 0 Then
            Do
                SFile = Left$(SFD.cFileName, InStr(SFD.cFileName, Chr(0)) - 1)
                If (SFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY Then
                    If (SFile <> ".") And (SFile <> "..") Then
                        Field(UBound(Field)) = SRoot & SFile
                        lngFileAttributes(UBound(Field)) = SFD.dwFileAttributes
                        Redim Preserve Field(0 To UBound(Field) + 1)
                        Redim Preserve lngFileAttributes&(0 To UBound(Field))
                    End If
                End If
            Loop While FindNextFile(ShFile, SFD)
            Call FindClose(ShFile)
        End If
    End If
    
    hFile = FindFirstFile(Root & strPath, FD)
    If hFile = 0 Then Exit Sub
    Do
        File = Left$(FD.cFileName, InStr(FD.cFileName, Chr(0)) - 1)
        xAttrib& = FD.dwFileAttributes
        If (FD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY Then
            If (File <> ".") And (File <> "..") Then
                SFile = File
                SRoot = Root
                GetAllDirctory Root & File, strPath, Field, lngFileAttributes, strSearchFile$, (strInstanz)
                If Right$(SFile, 1) <> "\" Then SFile = SFile & "\"
                SRoot = SRoot & SFile
                ShFile = FindFirstFile(SRoot & strSearchFile$, SFD)
                If ShFile > 0 Then
                    Do
                        SFile = Left$(SFD.cFileName, InStr(SFD.cFileName, Chr(0)) - 1)
                        If (SFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY Then
                            If (SFile <> ".") And (SFile <> "..") Then
                                Field(UBound(Field)) = SRoot & SFile
                                lngFileAttributes(UBound(Field)) = SFD.dwFileAttributes
                                Redim Preserve Field(0 To UBound(Field) + 1)
                                Redim Preserve lngFileAttributes&(0 To UBound(Field))
                            End If
                        End If
                    Loop While FindNextFile(ShFile, SFD)
                End If
            End If
            Call FindClose(ShFile)
        End If
    Loop While FindNextFile(hFile, FD)
    Call FindClose(hFile)
End Sub

Gruß
Nepumuk
Anzeige
AW: Problem Ordner einlesen Win8/ Excel 2013
30.09.2016 07:17:13
Volker
Wow, das ging ja wirklich fix!
Userbild
Ich kann wohl erst nächste Woche am lebenden Objekt testen, da ich dieses Problem auf einem Fremdrechner habe.
Spätestens Montag werde ich berichten.
Gruß Volker
AW: Problem Ordner einlesen Win8/ Excel 2013
05.10.2016 07:45:27
Volker
Hallo Nepumuk,
gestern konnte ich deinen Code testen und es läuft!!
Und nicht nur das, das Auslesen geht jetzt viel schneller!
Ich bin echt begeistert!!
Also noch einmal vielen Dank an dich!!
Userbild
Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige