ファイル名検索
MS Officeで提供されているFileSearchオブジェクトを使用。
Sub FoundFiles() ' /*----------------------*/ ' ファイル名検索 ' /*----------------------*/ ' ' c:\hogehogeフォルダの中のExcelファイル(拡張子.xls)を検索し、見つ ' かった場合は、ファイル名を表示する。 ' ' ' MS Office で提供しているオブジェクトのため Excel以外の MS Office ' でも使用可能。 ' ' ========= ' 変 数 ' ========= ' Dim folder_name As String ' 検索フォルダ名 Dim found_file As String ' 検索ファイル名 Dim i As Integer ' カウンタ ' ============ ' 初 期 値 ' ============ ' ' 検索フォルダ名指定 folder_name = "c:\hogehoge" ' ========= ' 処 理 ' ========= ' ' ファイル検索 With Application.FileSearch .NewSearch ' 検索条件のクリア .Filename = "*.xls" ' 検索ファイル名 .FileType = msoFileTypeExcelWorkbooks ' 検索ファイルタイプ .LookIn = folder_name ' 検索ディレクトリ名 .SearchSubFolders = False ' サブフォルダの検索の有無 .Execute ' 検索開始 ' 検索ファイルが見つかった場合 If .Execute() > 0 Then ' 検索ファイル名取得 For i = 1 To .FoundFiles.Count ' 検索ファイル名取得 found_file = Dir(.FoundFiles(i), vbNormal) MsgBox found_file & "が見つかりました。" Next i Else ' 検索ファイルが見つからなかった場合 MsgBox "検索条件を満たすファイルはありません。" End If End With End Sub