3

次のコードをExcelに入れました。ネットで見つけたスクリプトです。

   Sub TestListFolders()
        Application.ScreenUpdating = False
        Workbooks.Add ' create a new workbook for the folder list
        ' add headers
        With Range("A1")
            .Formula = "Folder contents:"
            .Font.Bold = True
            .Font.Size = 12
        End With
        Range("A3").Formula = "Folder Path:"
        Range("B3").Formula = "Folder Name:"
        Range("C3").Formula = "Size:"
        Range("D3").Formula = "Subfolders:"
        Range("E3").Formula = "Files:"
        Range("F3").Formula = "Short Name:"
        Range("G3").Formula = "Short Path:"
        Range("A3:G3").Font.Bold = True
        ListFolders "C:\FolderName\", True
        Application.ScreenUpdating = True
    End Sub

Sub ListFolders(SourceFolderName As String, IncludeSubfolders As Boolean)
' lists information about the folders in SourceFolder
' example: ListFolders "C:\FolderName", True
Dim FSO As Scripting.FileSystemObject
Dim SourceFolder As Scripting.Folder, SubFolder As Scripting.Folder
Dim r As Long
    Set FSO = New Scripting.FileSystemObject
    Set SourceFolder = FSO.GetFolder(SourceFolderName)
    ' display folder properties
    r = Range("A65536").End(xlUp).Row + 1
    Cells(r, 1).Formula = SourceFolder.Path
    Cells(r, 2).Formula = SourceFolder.Name
    Cells(r, 3).Formula = SourceFolder.Size
    Cells(r, 4).Formula = SourceFolder.SubFolders.Count
    Cells(r, 5).Formula = SourceFolder.Files.Count
    Cells(r, 6).Formula = SourceFolder.ShortName
    Cells(r, 7).Formula = SourceFolder.ShortPath
    If IncludeSubfolders Then
        For Each SubFolder In SourceFolder.SubFolders
            ListFolders SubFolder.Path, True
        Next SubFolder
        Set SubFolder = Nothing
    End If
    Columns("A:G").AutoFit
    Set SourceFolder = Nothing
    Set FSO = Nothing
    ActiveWorkbook.Saved = True
End Sub

このオブジェクトが見つからないため、スクリプトは失敗します。

New Scripting.FileSystemObject

オブジェクトのライブラリを取得するにはどうすればよいですか? そのオブジェクトに依存しない、代わりに使用できる別のスクリプトはありますか?

4

2 に答える 2

3

VBA 自体には、ファイル システムにアクセスするための関数が組み込まれていますDirが (例: )、使用するのはかなり面倒です。

上記のコードを機能させるには、 Microsoft Scripting Runtimeへの参照 ( Tools->References ) を追加するだけです。

于 2013-07-25T09:21:13.587 に答える