Home >> ヒント・サンプル集 >> プログラムサンプル >> (VBA[関数] フォルダ選択 ダイアログ

<VBA> [関数] フォルダ選択 ダイアログ
フォルダを選択するためのダイアログです

    Declare PtrSafe Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As LongPtr
    Declare PtrSafe Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As LongPtr, ByVal pszPath As String) As Boolean

    Public Type BROWSEINFO
        hOwner As LongPtr
        pidlRoot As LongPtr
        pszDisplayName As String
        lpszTitle As String
        ulFlags As Long
        lpfn As LongPtr
        lParam As LongPtr
        iImage As Long
    End Type
    
Function saGetFolder(Title, Optional NewBTN) As String
    Dim bInfo As BROWSEINFO, pPath As String
    Dim r As Boolean, POS As Integer
    Dim pidl
    
    If IsMissing(NewBTN) Then NewBTN = True
    
    bInfo.pidlRoot = 0&
    
    bInfo.lpszTitle = Title
    If NewBTN = True Then
        bInfo.ulFlags = &H41
    Else
        bInfo.ulFlags = &H241
    End If
    pidl = SHBrowseForFolder(bInfo)
    pPath = Space$(512)
    r = SHGetPathFromIDList(ByVal pidl, ByVal pPath)
    If r Then
        POS = InStr(pPath, Chr$(0))
        saGetFolder = Left(pPath, POS - 1)
    Else
        saGetFolder = ""
    End If
End Function


上記コードをModuleに貼り付けます

【引数】ダイアログタイトル、(フォルダ作成ボタンの表示有無=オプション)

【サンプル】
wk = saGetFolder("ダイアログのタイトル", False)
If wk <> "" Then Me!Text1 = wk

【特記事項】
キャンセルボタンをクリックしたときには、文字長さゼロの文字が返ります

  お気軽にご相談ください お問合せ・ご相談はこちら お問合せ・ご相談はこちら  
更新日:2017/02/21 14:22