ファイル/フォルダ選択ダイアログ、ファイルサーチの例
ファイル選択ダイアログ
Private Sub ctlBtn_Click() Dim fd As FileDialog Set fd = Application.FileDialog(msoFileDialogFilePicker) With fd .Title = "ファイル選択" .AllowMultiSelect = False .Filters.Clear .Filters.Add "エクセルファイル", "*.xls" If .Show = -1 Then ctlTextbox = .SelectedItems(1) End If End With End Sub
フォルダ選択ダイアログ
Private Sub ctlBtn1_Click() Dim fd As FileDialog Set fd = Application.FileDialog(msoFileDialogFolderPicker) With fd .Title = "フォルダ選択" .AllowMultiSelect = False .Filters.Clear If .Show = -1 Then ctlDir = .SelectedItems(1) End If End With End Sub
ファイルサーチ
Private Sub ctlBtn2_Click() Set fs = Application.FileSearch With fs .NewSearch .LookIn = ctlDir .Filename = ctlName .SearchSubFolders = True If .Execute(SortBy:=msoSortByLastModified, SortOrder:=msoSortOrderDescending) > 0 Then MsgBox "見つかったファイルは " & .FoundFiles.Count & _ " ファイルです。" For i = 1 To .FoundFiles.Count MsgBox .FoundFiles(i) Next i Else MsgBox "ファイルが見つかりません。" End If End With End Sub