admin管理员组

文章数量:1122796

I'm parsing folders & subfolders for certain files with text in them and getting the results returned, but I keep getting "error 380 could not set the list property". I Redimemd my confList array to match the File results but can't see what I'm missing. Any help is appreciated!

Sub searchConf(Folder)
    Dim SubFolder
    Dim File
    Dim confList()
    Dim searchFullName As String
    Dim i As Integer
    Dim FSO As Object
    Set FSO = CreateObject("Scripting.FileSystemObject")
        
    i = 0
        
    searchFullName = dwgText1.Value
    
    For Each SubFolder In Folder.SubFolders
        searchConf SubFolder
        If SubFolder.Name Like searchFullName Then
            i = i + 1
            ReDim confList(1 To SubFolder.Files.Count, 1 To 3)
            For Each File In SubFolder.Files
                confList(i, 1) = File.Name
                confList(i, 2) = FSO.GetExtensionName(LCase(File))
                confList(i, 3) = File.path
            Next
        End If
    Next
        
    With list2
        .Clear
        .ColumnCount = 2
        .ColumnWidths = "146;20"
        .List = confList
    End With
    
    If list2.List(0) = "" Then
        With list2
            .Clear
            .AddItem "NO FILES FOUND"
        End With
    End If
    
End Sub

The above Sub is called from:

Sub getConf()
    Dim FileSystem As Object
    Dim dataSplit() As String
    Dim firstFolder, searchFullName As String
    
    searchFullName = dwgText1.Value
    
    dataSplit = Split(searchFullName, "-")
    firstFolder = dataSplit(0)
    
    folderName = folderName & "\" & firstFolder
        
    Set FileSystem = CreateObject("Scripting.FileSystemObject")
    searchConf FileSystem.GetFolder(folderName)

End Sub

I tried what you suggested but still getting the "308 error, could not set the list property". I confirmed the file count in the folder and that they were results from the for loop:

If Folder.Files.Count <> 0 Then
    ReDim confList(1 To Folder.Files.Count, 1 To 3)
End If

For Each SubFolder In Folder.SubFolders
    searchConf SubFolder
    If SubFolder.Name Like searchFullName Then
        Debug.Print SubFolder.Files.Count
        ReDim confList(1 To SubFolder.Files.Count, 1 To 3)
        i = i + 1
        For Each File In SubFolder.Files
            confList(i, 1) = File.Name
            confList(i, 2) = FSO.GetExtensionName(LCase(File))
            confList(i, 3) = File.path
        Next
    End If
Next
    
With list2
    .Clear
    .ColumnCount = 2
    .ColumnWidths = "146;20"
    .List = confList
End With

It generates the error at the ".List = confList" line. Am I not calling the array correctly there or is the problem with the array itself?

本文标签: vbaRedim Array for ListBoxStack Overflow