フォルダ選択
' ==================================================================
' = 概要 フォルダ選択ダイアログを表示する
' = 引数 sInitPath String [in] デフォルトフォルダパス(省略可)
' = 戻値 String フォルダ選択結果
' = 覚書 なし
' ==================================================================
Private Function ShowFolderSelectDialog( _
Optional ByVal sInitPath As String = "" _
) As String
Dim fdDialog As Office.FileDialog
Set fdDialog = Application.FileDialog(msoFileDialogFolderPicker)
fdDialog.Title = "フォルダを選択してください"
If sInitPath = "" Then
'Do Nothing
Else
If Right(sInitPath, 1) = "\" Then
fdDialog.InitialFileName = sInitPath
Else
fdDialog.InitialFileName = sInitPath & "\"
End If
End If
'ダイアログ表示
Dim lResult As Long
lResult = fdDialog.Show()
If lResult <> -1 Then 'キャンセル押下
ShowFolderSelectDialog = ""
Else
Dim sSelectedPath As String
sSelectedPath = fdDialog.SelectedItems.Item(1)
If CreateObject("Scripting.FileSystemObject").FolderExists(sSelectedPath) Then
ShowFolderSelectDialog = sSelectedPath
Else
ShowFolderSelectDialog = ""
End If
End If
Set fdDialog = Nothing
End Function
Private Sub Test_ShowFolderSelectDialog()
Dim objWshShell
Set objWshShell = CreateObject("WScript.Shell")
MsgBox ShowFolderSelectDialog( _
objWshShell.SpecialFolders("Desktop") _
)
End Sub
ファイル選択
' ==================================================================
' = 概要 ファイル(単一)選択ダイアログを表示する
' = 引数 sInitPath String [in] デフォルトファイルパス(省略可)
' = 引数 sFilters String [in] 選択時のフィルタ(省略可)(※)
' = 戻値 String ファイル選択結果
' = 覚書 (※)ダイアログのフィルタ指定方法は以下。
' = ex) 画像ファイル/*.gif; *.jpg; *.jpeg,テキストファイル/*.txt; *.csv
' = ・拡張子が複数ある場合は、";"で区切る
' = ・ファイル種別と拡張子は"/"で区切る
' = ・フィルタが複数ある場合、","で区切る
' = sFilters が省略もしくは空文字の場合、フィルタをクリアする。
' ==================================================================
Private Function ShowFileSelectDialog( _
Optional ByVal sInitPath As String = "", _
Optional ByVal sFilters As String = "" _
) As String
Dim fdDialog As Office.FileDialog
Set fdDialog = Application.FileDialog(msoFileDialogFilePicker)
fdDialog.Title = "ファイルを選択してください"
fdDialog.AllowMultiSelect = False
If sInitPath = "" Then
'Do Nothing
Else
fdDialog.InitialFileName = sInitPath
End If
Call SetDialogFilters(sFilters, fdDialog) 'フィルタ追加
'ダイアログ表示
Dim lResult As Long
lResult = fdDialog.Show()
If lResult <> -1 Then 'キャンセル押下
ShowFileSelectDialog = ""
Else
Dim sSelectedPath As String
sSelectedPath = fdDialog.SelectedItems.Item(1)
If CreateObject("Scripting.FileSystemObject").FileExists(sSelectedPath) Then
ShowFileSelectDialog = sSelectedPath
Else
ShowFileSelectDialog = ""
End If
End If
Set fdDialog = Nothing
End Function
Private Sub Test_ShowFileSelectDialog()
Dim objWshShell
Set objWshShell = CreateObject("WScript.Shell")
Dim sFilters As String
'sFilters = "画像ファイル/*.gif; *.jpg; *.jpeg; *.png"
'sFilters = "画像ファイル/*.gif; *.jpg; *.jpeg,テキストファイル/*.txt; *.csv"
'sFilters = "画像ファイル/*.gif; *.jpg; *.jpeg; *.png,テキストファイル/*.txt; *.csv"
sFilters = ""
MsgBox ShowFileSelectDialog( _
objWshShell.SpecialFolders("Desktop") & "\test.txt", _
sFilters _
)
' MsgBox ShowFileSelectDialog( _
' objWshShell.SpecialFolders("Desktop") & "\test.txt" _
' )
End Sub
' ==================================================================
' = 概要 ファイル(複数)選択ダイアログを表示する
' = 引数 asSelectedFiles String() [out] 選択されたファイルパス一覧
' = 引数 sInitPath String [in] デフォルトファイルパス(省略可)
' = 引数 sFilters String [in] 選択時のフィルタ(省略可)(※)
' = 戻値 なし
' = 覚書 (※)ダイアログのフィルタ指定方法は以下。
' = ex) 画像ファイル/*.gif; *.jpg; *.jpeg,テキストファイル/*.txt; *.csv
' = ・拡張子が複数ある場合は、";"で区切る
' = ・ファイル種別と拡張子は"/"で区切る
' = ・フィルタが複数ある場合、","で区切る
' = sFilters が省略もしくは空文字の場合、フィルタをクリアする。
' ==================================================================
Private Function ShowFilesSelectDialog( _
ByRef asSelectedFiles() As String, _
Optional ByVal sInitPath As String = "", _
Optional ByVal sFilters As String = "" _
)
Dim fdDialog As Office.FileDialog
Set fdDialog = Application.FileDialog(msoFileDialogFilePicker)
fdDialog.Title = "ファイルを選択してください(複数可)"
fdDialog.AllowMultiSelect = True
If sInitPath = "" Then
'Do Nothing
Else
fdDialog.InitialFileName = sInitPath
End If
Call SetDialogFilters(sFilters, fdDialog) 'フィルタ追加
'ダイアログ表示
Dim lResult As Long
lResult = fdDialog.Show()
If lResult <> -1 Then 'キャンセル押下
ReDim Preserve asSelectedFiles(0)
asSelectedFiles(0) = ""
Else
Dim lSelNum As Long
lSelNum = fdDialog.SelectedItems.Count
ReDim Preserve asSelectedFiles(lSelNum - 1)
Dim lSelIdx As Long
For lSelIdx = 0 To lSelNum - 1
Dim sSelectedPath As String
sSelectedPath = fdDialog.SelectedItems(lSelIdx + 1)
If CreateObject("Scripting.FileSystemObject").FileExists(sSelectedPath) Then
asSelectedFiles(lSelIdx) = sSelectedPath
Else
asSelectedFiles(lSelIdx) = ""
End If
Next lSelIdx
End If
Set fdDialog = Nothing
End Function
Private Sub Test_ShowFilesSelectDialog()
Dim objWshShell
Set objWshShell = CreateObject("WScript.Shell")
Dim sFilters As String
'sFilters = "画像ファイル/*.gif; *.jpg; *.jpeg; *.png"
'sFilters = "画像ファイル/*.gif; *.jpg; *.jpeg,テキストファイル/*.txt; *.csv"
'sFilters = "画像ファイル/*.gif; *.jpg; *.jpeg; *.png,テキストファイル/*.txt; *.csv"
sFilters = "全てのファイル/*.*,画像ファイル/*.gif; *.jpg; *.jpeg; *.png,テキストファイル/*.txt; *.csv"
Dim asSelectedFiles() As String
Call ShowFilesSelectDialog( _
asSelectedFiles, _
objWshShell.SpecialFolders("Desktop") & "\test.txt", _
sFilters _
)
Dim sBuf As String
sBuf = ""
sBuf = sBuf & vbNewLine & UBound(asSelectedFiles) + 1
Dim lSelIdx As Long
For lSelIdx = 0 To UBound(asSelectedFiles)
sBuf = sBuf & vbNewLine & asSelectedFiles(lSelIdx)
Next lSelIdx
MsgBox sBuf
End Sub
'ShowFileSelectDialog() と ShowFilesSelectDialog() 用の関数
'ダイアログのフィルタを追加する。指定方法は以下。
' ex) 画像ファイル/*.gif; *.jpg; *.jpeg,テキストファイル/*.txt; *.csv
' ・拡張子が複数ある場合は、";"で区切る
' ・ファイル種別と拡張子は"/"で区切る
' ・フィルタが複数ある場合、","で区切る
'sFilters が空文字の場合、フィルタをクリアする。
Private Function SetDialogFilters( _
ByVal sFilters As String, _
ByRef fdDialog As FileDialog _
)
fdDialog.Filters.Clear
If sFilters = "" Then
'Do Nothing
Else
Dim vFilter As Variant
If InStr(sFilters, ",") > 0 Then
Dim vFilters As Variant
vFilters = Split(sFilters, ",")
Dim lFilterIdx As Long
For lFilterIdx = 0 To UBound(vFilters)
If InStr(vFilters(lFilterIdx), "/") > 0 Then
vFilter = Split(vFilters(lFilterIdx), "/")
If UBound(vFilter) = 1 Then
fdDialog.Filters.Add vFilter(0), vFilter(1), lFilterIdx + 1
Else
MsgBox _
"ファイル選択ダイアログのフィルタの指定方法が誤っています" & vbNewLine & _
"""/"" は一つだけ指定してください" & vbNewLine & _
" " & vFilters(lFilterIdx)
MsgBox "処理を中断します。"
End
End If
Else
MsgBox _
"ファイル選択ダイアログのフィルタの指定方法が誤っています" & vbNewLine & _
"種別と拡張子を ""/"" で区切ってください。" & vbNewLine & _
" " & vFilters(lFilterIdx)
MsgBox "処理を中断します。"
End
End If
Next lFilterIdx
Else
If InStr(sFilters, "/") > 0 Then
vFilter = Split(sFilters, "/")
If UBound(vFilter) = 1 Then
fdDialog.Filters.Add vFilter(0), vFilter(1), 1
Else
MsgBox _
"ファイル選択ダイアログのフィルタの指定方法が誤っています" & vbNewLine & _
"""/"" は一つだけ指定してください" & vbNewLine & _
" " & sFilters
MsgBox "処理を中断します。"
End
End If
Else
MsgBox _
"ファイル選択ダイアログのフィルタの指定方法が誤っています" & vbNewLine & _
"種別と拡張子を ""/"" で区切ってください。" & vbNewLine & _
" " & sFilters
MsgBox "処理を中断します。"
End
End If
End If
End If
End Function
ファイル選択(パターン2)(未使用)
Dim vFilePath As Variant
vFilePath = Application.GetOpenFilename( _
Title:="ファイル選択ウィンドウ", _
FileFilter:="エクセルファイル(*.xls),*.xls,CSVファイル(*.csv),*.csv", _
FilterIndex:=1, _
MultiSelect:=True _
)
If IsArray(vFilePath) Then
Debug.Print UBound(vFilePath)
Else
Debug.Print 0
End If
最終更新:2017年03月21日 12:08