ダイアログ フォルダ選択ダイアログを表示し選択パスを返す

※より実装に近く表示させる為、コードの改行を避けています。スマホ等で閲覧される際は向きを変えてご覧ください。

※実装するバージョンによってはバージョンアップの仕様により動作しないコードもあります。実装には動作確認の上ご使用下さい。

Option Explicit


Public Function fncDialogFolder(strMsg As StringAs String '全ドライブ
'**********************************************
'フォルダ選択ダイアログを表示し選択パスを返す
'**********************************************
Dim Shell, myPath, str As String

    Set Shell = CreateObject("Shell.Application")
    Set myPath = Shell.BrowseForFolder(&O0, strMsg & _
    "をするフォルダを選んでください", &H1 + &H10, "")
    If Not myPath Is Nothing Then
    str = myPath.Items.Item.Path & "\"
    Else 'キャンセルが押された場合の処置
    str = ""
    End If
    Set Shell = Nothing
    Set myPath = Nothing
    fncDialogFolder = str
End Function


Function BrowseFolder() As String
'**********************************************
'フォルダ選択ダイアログを表示し選択パスを返す
'**********************************************

Dim Shell
    Set Shell = CreateObject("Shell.Application") _
    .BrowseForFolder(&O0, "フォルダ選択", &H1 + &H10, 0)

    If Shell Is Nothing Then
        MsgBox "フォルダは選択されませんでした!", vbCritical
        BrowseFolder = ""
        Exit Function
    Else
        BrowseFolder = Shell.Items.Item.Path & "\"
    End If

End Function


Private Sub test()
    MsgBox BrowseFolder
End Sub

 

2000年01月01日|[VBサンプルコード]:[ダイアログ]