昨日は、ユーザーフォーム上の階層別リストボックスにフォルダ名を表示し、それを順次選択して絞り込みを行った。
さて、今日はいよいよファイル名までを表示させて選択できるようにする。
といっても、やっていることは昨日までと変わらない。フォルダが、ファイルになっただけだ。
クラスモジュール(SeaquenceClass)
配列で取得する場合。
Public Function GetFilesSeq(folder_path As String) As Variant Dim TempCol As Collection Set TempCol = GetFilesCol(folder_path) If TempCol.Count >= 1 Then GetFilesSeq = ToArray(TempCol) Else GetFilesSeq = Array() End If End Function
コレクションで取得する場合。
Public Function GetFilesCol(folder_path As String) As Collection ' Microsoft Scripting Runtimeの参照設定要 Dim FSO As FileSystemObject Set FSO = New FileSystemObject Dim TempCol As Collection Set TempCol = New Collection Dim myFile As File For Each myFile In FSO.GetFolder(folder_path).Files TempCol.Add myFile.Name Next Set GetFilesCol = TempCol End Function
ユーザーフォーム(SelectFileForm)
上記で作成したユーザー定義関数を、ユーザーフォームに反映する。
なお、昨日の繰り返しになるが、各リストボックスの内訳は下記のとおり。
- 左:FolderListBox1
- 中:FolderListBox2
- 右:FileListBox
真ん中のリストボックス。
Private Sub FolderListBox2_Click() Dim FullPath As String FullPath = FolderPath & "\" & _ FolderListBox1.Value & "\" & _ FolderListBox2.Value With FileListBox .Clear .List = SQC.GetFilesSeq(FullPath) FolderPathLabel = FullPath End With End Sub
右のリストボックス。
Private Sub FileListBox_Click() Dim LabelCol As Collection Set LabelCol = New Collection LabelCol.Add FolderPath LabelCol.Add FolderListBox1.Value LabelCol.Add FolderListBox2.Value LabelCol.Add FileListBox.Value FolderPathLabel = Join(SQC.ToArray(LabelCol), "\") End Sub
結果は、以下のとおり。
予定した動きは実現できた。しかし、各プロシージャに似たような記述がたくさんあって、もっさり感がある。
そこで明日は、これらを統合して綺麗にまとめることに挑戦する。
参考まで。