使用FileSystemObject列出文件获取错误

时间:2015-09-02 17:25:09

标签: excel vba excel-vba

我有Excel-2007。我正在使用文件系统对象VBA代码列出目录中的文件。我还在excel中设置了对Microsoft Scriptlet库的引用。 我得到了:

Compiler error:User-defined type not defined

在第一个代码行

Dim FSO As Scripting.FileSystemObject

我使用的代码如下:

 Sub ListFilesinFolder()

    Dim FSO As Scripting.FileSystemObject
    Dim SourceFolder As Scripting.Folder
    Dim FileItem As Scripting.File

    SourceFolderName = "C:\mydir"

    Set FSO = New Scripting.FileSystemObject
    Set SourceFolder = FSO.GetFolder(SourceFolderName)

    Range("A1:C1") = Array("text file", "path", "Date Last Modified")

    i = 2
    For Each FileItem In SourceFolder.Files
        Cells(i, 1) = FileItem.Name
        Cells(i, 2) = FileItem
        Cells(i, 3) = FileItem.DateLastModified
        i = i + 1
    Next FileItem

    Set FSO = Nothing

End Sub

有人可以指出我哪里出错吗?

      **UPDATE -03-09-2015**   

我已根据@brettdj程序和一些研究更新了我的程序,以列出包括子文件夹文件在内的所有文件。这个对我有用。我期待着进一步改进它的建议。

      Sub ListFilesinFolder()
           Dim objFSO As Object
           Dim ws As Worksheet
           Dim cl As Range
           Dim objFolderName As String

           objFolderName = "C:\FY_2015-2016\sunil"
           Set objFSO = New Scripting.FileSystemObject

           Set ws = ActiveSheet 
           With Range("A1:C1")
             .Value2 = Array("File", "path", "Date Last Modified")
             .Font.Bold = True
           End With

           Set cl = ws.Cells(2, 1)

           ListFolders cl, objFSO.GetFolder(objFolderName)
           Set objFSO = Nothing
    End Sub

    Sub ListFolders(rng As Range, Fol As Scripting.Folder)
          Dim SubFol As Scripting.Folder
          Dim FileItem As Scripting.File
          ' List Files
          For Each FileItem In Fol.Files
             rng.Cells(1, 1) = FileItem.Name
             rng.Cells(1, 2) = FileItem.ParentFolder.Path
             rng.Cells(1, 3) = FileItem.DateLastModified
             Set rng = rng.Offset(1, 0)
          Next
        ' Proces subfolders
         For Each SubFol In Fol.SubFolders
              ListFolders rng, SubFol
         Next
         With ActiveSheet
            .Columns.EntireColumn.AutoFit
         End With
    End Sub

我发布的另一个更新不是逐个单元格填充。      2015年3月3日修订更新

  Sub GetFileList()

    Dim strFolder As String
    Dim objFSO As Object
    Dim objFolder As Object
    Dim myResults As Variant
    Dim lCount As Long

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    ' Get the directory from the user
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Show
        If .SelectedItems.Count = 0 Then Exit Sub
        'user cancelled
        strFolder = .SelectedItems(1)
    End With

    Set objFolder = objFSO.GetFolder(strFolder)

    'the variable dimension has to be the second one
    ReDim myResults(0 To 5, 0 To 0)

    ' place make some headers in the array
    myResults(0, 0) = "Filename"
    myResults(1, 0) = "Size"
    myResults(2, 0) = "Created"
    myResults(3, 0) = "Modified"
    myResults(4, 0) = "Accessed"
    myResults(5, 0) = "Full path"

    'Send the folder to the recursive function
    FillFileList objFolder, myResults, lCount

    ' Dump these to a worksheet
    fcnDumpToWorksheet myResults

    'tidy up
    Set objFSO = Nothing

    End Sub

Private Sub FillFileList(objFolder As Object, ByRef myResults As Variant, ByRef lCount As Long, Optional strFilter As String)

    Dim i As Integer
    Dim objFile As Object
    Dim fsoSubFolder As Object
    Dim fsoSubFolders As Object

    'load the array with all the files
    For Each objFile In objFolder.Files
        lCount = lCount + 1
        ReDim Preserve myResults(0 To 5, 0 To lCount)
        myResults(0, lCount) = objFile.Name
        myResults(1, lCount) = objFile.Size
        myResults(2, lCount) = objFile.DateCreated
        myResults(3, lCount) = objFile.DateLastModified
        myResults(4, lCount) = objFile.DateLastAccessed
        myResults(5, lCount) = objFile.Path
    Next objFile

    'recursively call this function with any subfolders
    Set fsoSubFolders = objFolder.SubFolders

    For Each fsoSubFolder In fsoSubFolders
        FillFileList fsoSubFolder, myResults, lCount
    Next fsoSubFolder

End Sub

Private Sub fcnDumpToWorksheet(varData As Variant, Optional mySh As Worksheet)

    Dim iSheetsInNew As Integer
    Dim sh As Worksheet, wb As Workbook
    Dim myColumnHeaders() As String
    Dim l As Long, NoOfRows As Long

    If mySh Is Nothing Then
        'make a workbook if we didn't get a worksheet
        iSheetsInNew = Application.SheetsInNewWorkbook
        Application.SheetsInNewWorkbook = 1
        Set wb = Application.Workbooks.Add
        Application.SheetsInNewWorkbook = iSheetsInNew
        Set sh = wb.Sheets(1)
    Else
        Set mySh = sh
    End If

    'since we switched the array dimensions, have to transpose
    With sh
        Range(.Cells(1, 1), .Cells(UBound(varData, 2) + 1, UBound(varData, 1) + 1)) = _
            Application.WorksheetFunction.Transpose(varData)

        .UsedRange.Columns.AutoFit
    End With

    Set sh = Nothing
    Set wb = Nothing

End Sub

3 个答案:

答案 0 :(得分:1)

您正在引用Microsoft Scriptlet Library;应该是Microsoft Scripting Runtime。

答案 1 :(得分:1)

试试这个:

Sub ListFilesinFolder()

    Dim FSO
    Dim SourceFolder
    Dim FileItem

    SourceFolderName = "C:\mydir"

    Set FSO = CreateObject("Scripting.FileSystemObject") '<-- New change
    Set SourceFolder = FSO.GetFolder(SourceFolderName)

    Range("A1:C1") = Array("text file", "path", "Date Last Modified")

    i = 2
    For Each FileItem In SourceFolder.Files
        Cells(i, 1) = FileItem.Name
        Cells(i, 2) = FileItem
        Cells(i, 3) = FileItem.DateLastModified
        i = i + 1
    Next FileItem

    Set FSO = Nothing

End Sub

答案 2 :(得分:1)

建议使用数组方法来提高速度

Sub ListFilesinFolder()

    Dim objFSO As Object
    Dim objFolder As Object
    Dim objFile As Object
    Dim lngCnt As Long
    Dim X

    objFolderName = "C:\temp"

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(objFolderName)


    ReDim X(1 To objFolder.Files.Count, 1 To 3)

    For Each objFile In objFolder.Files
        lngCnt = lngCnt + 1
        X(lngCnt, 1) = objFile.Name
        X(lngCnt, 2) = objFile.Path
        X(lngCnt, 3) = Format(objFile.DateLastModified, "dd-mmm-yyyy")
    Next

    [a2].Resize(UBound(X, 1), 3).Value2 = X

    With Range("A1:C1")
        .Value2 = Array("text file", "path", "Date Last Modified")
        .Font.Bold = True
        .Columns.EntireColumn.AutoFit
    End With

End Sub