Vba 使用FileSystemObject列出文件获取错误

Vba 使用FileSystemObject列出文件获取错误,vba,excel,Vba,Excel,我有Excel-2007。我正在使用文件系统对象VBA代码列出目录中的文件。我还在excel中设置了对Microsoft Scriptlet库的引用。 我得到: 编译器错误:未定义用户定义的类型 在这第一行代码中 Dim FSO作为脚本。FileSystemObject 我使用的代码如下: Sub ListFilesinFolder() Dim FSO As Scripting.FileSystemObject Dim SourceFolder As Scripting.Fo

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

编译器错误:未定义用户定义的类型

在这第一行代码中

Dim FSO作为脚本。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年9月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

您正在引用Microsoft Scriptlet库;应该是Microsoft脚本运行时。

请尝试以下操作:

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
子列表filesinfolder()
模糊FSO
Dim SourceFolder
Dim文件项
SourceFolderName=“C:\mydir”

Set FSO=CreateObject(“Scripting.FileSystemObject”)”建议使用数组方法提高速度

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

您希望获得所有文件详细信息,还是仅获取txt文件详细信息。请在第二阶段获取所有文件详细信息。目前仅作为建立和验证程序代码的示例来处理txt文件。@skkhar不逐个填充数据单元格,请像我一样使用数组。如果您能更新您的答案,列出目录和子目录中的所有文件,我将不胜感激,这将有助于我正确放置我未来程序的代码。感谢一个递归过程,看看从你的程序和一些研究中得到的提示,我已经更新了程序,列出了目录和子目录中的所有文件。期待您的意见进一步完善和改进。非常感谢。如果您能更新您的答案,列出目录和子目录中的所有文件,我将不胜感激,这将有助于我正确地为我未来的程序放置代码。ThanksI发布了修订版更新,该更新不是基于逐个单元格的填充方法。希望能得到你的认可