ich möchte eine Ordnerstruktur in Excel importieren und bin im Forum auch fündig geworden.
Leider funktioniert der Code nicht. Kann mir jemand sagen, was damit los ist.?
der Fehler wird in folgender Zeile angezeigt, nur sagt mir das nicht viel:
With wksInhalt
.Range(.Cells(2, 1), .Cells(lngFiles, UBound(vntFiles, 1))) = WorksheetFunction.Transpose(vntFiles)
Ich danke euch schon mal
Gruß Alex
Option Explicit
Dim wksInhalt As Worksheet, vntFiles(), lngFiles As Long
Public Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, _
ByVal pszPath As String) As Long
Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
Sub DateiListe()
Dim FSO As Object, oFolder As Object
Dim strFolder As String
Application.ScreenUpdating = False
Set wksInhalt = ThisWorkbook.Sheets("Tabelle1")
Set FSO = CreateObject("Scripting.FileSystemObject")
strFolder = GetDirectory
If strFolder = "" Then Exit Sub
Set oFolder = FSO.getfolder(strFolder)
lngFiles = 1
With wksInhalt
.Cells.ClearContents
.Cells(1, 1) = "Name"
.Cells(1, 2) = "Ext"
.Cells(1, 3) = "Bemerkung"
.Cells(1, 4) = "Ordner"
.Cells(1, 5) = "kB"
.Cells(1, 6) = "le.Änd."
.Cells(1, 7) = "Erstellt"
.Cells(1, 8) = "Pfad"
.Cells(1, 9) = "Link"
.Range(.Cells(1, 1), .Cells(1, 5)).Font.Bold = True
End With
prcFiles oFolder
prcSubFolders oFolder
With wksInhalt
.Range(.Cells(2, 1), .Cells(lngFiles, UBound(vntFiles, 1))) = WorksheetFunction. _
Transpose(vntFiles)
.Activate
End With
Application.ScreenUpdating = True
End Sub
Sub prcSubFolders(oFolder)
Dim oSubFolder As Object
For Each oSubFolder In oFolder.subfolders
prcFiles oSubFolder
prcSubFolders oSubFolder
Next
End Sub
Sub prcFiles(oFolder)
Dim oFile As Object
For Each oFile In oFolder.Files
ReDim Preserve vntFiles(1 To 9, 1 To lngFiles)
vntFiles(2, lngFiles) = GetExtension(oFile.Name)
vntFiles(1, lngFiles) = Left(oFile.Name, Len(oFile.Name) - Len(vntFiles(2, lngFiles)) - _
_
1)
vntFiles(4, lngFiles) = oFolder
vntFiles(5, lngFiles) = Int(oFile.Size / 1024)
vntFiles(6, lngFiles) = oFile.datelastmodified
vntFiles(7, lngFiles) = oFile.datecreated
vntFiles(8, lngFiles) = oFile.Path
vntFiles(9, lngFiles) = "=hyperlink(""" & oFile.Path & """;""" & "Klick" & """)"
lngFiles = lngFiles + 1
Next
End Sub
Private Function GetExtension(strFile As String) As String
If InStrRev(strFile, ".") > 0 Then
GetExtension = Right(strFile, Len(strFile) - InStrRev(strFile, "."))
Else
GetExtension = ""
End If
End Function
Function GetDirectory(Optional Msg) As String
Dim bInfo As BROWSEINFO
Dim Path As String
Dim R As Long, x As Long, pos As Integer
bInfo.pidlRoot = 0&
If IsMissing(Msg) Then
bInfo.lpszTitle = "Wählen Sie bitte einen Ordner aus."
Else
bInfo.lpszTitle = Msg
End If
bInfo.ulFlags = &H1
x = SHBrowseForFolder(bInfo)
Path = Space$(512)
R = SHGetPathFromIDList(ByVal x, ByVal Path)
If R Then
pos = InStr(Path, Chr$(0))
GetDirectory = Left(Path, pos - 1)
Else
GetDirectory = ""
End If
End Function