need select path dialog userform

need select path dialog userform

Anonymous
Not applicable
603 Views
2 Replies
Message 1 of 3

need select path dialog userform

Anonymous
Not applicable

Okay,

 

I am trying to get a dialog box that i can select a path from. Im using it to make a script writer in vba. I have tried a few different ways. Ive tried using the "microsoft common dialog box" it says i dont have a license to do it.. or something... the other way is to add a reference through tools, and with like "textbox5.text" something that made no sense... does anyone have any idea of like a starting point on where to figure it out??? because im at a loss..

 

 

ps. sorry about the HORRIFIC gramer and spelling 🙂

0 Likes
604 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable

hi try this maybe.. 

 

Function BrowseFolder(Optional Caption As String = "") As String

Dim BrowseInfo As BrowseInfo
Dim FolderName As String
Dim ID As Long
Dim Res As Long

With BrowseInfo
.hOwner = 0
.pidlRoot = 0
.pszDisplayName = String$(MAX_PATH, vbNullChar)
.lpszINSTRUCTIONS = Caption
.ulFlags = BIF_RETURNONLYFSDIRS
.lpfn = 0
End With

FolderName = String$(MAX_PATH, vbNullChar)

ID = SHBrowseForFolderA(BrowseInfo)

If ID Then
Res = SHGetPathFromIDListA(ID, FolderName)
If Res Then
BrowseFolder = Left$(FolderName, InStr(FolderName, _
vbNullChar) - 1)
End If
End If

End Function

 

Sub TEST()


'Dim fname As String
fName = BrowseFolder("Select A Folder")
If fName = "" Then
MsgBox "You didn't select a folder"
Else
MsgBox "You selected: " & fName
End If

End Sub

 

0 Likes
Message 3 of 3

Anonymous
Not applicable

Run "GetPasta" procedure.

 

'Declarações API
#If VBA7 And Win64 Then
    Private 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
                        
    Private Declare PtrSafe Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" _
        (lpBrowseInfo As BROWSEINFO) As LongPtr
    Private Declare PtrSafe Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" _
        (ByVal pidl As LongPtr, ByVal pszPath As String) As Boolean
#Else
    Private Type BROWSEINFO
        hOwner As Long
        pidlRoot As Long
        pszDisplayName As String
        lpszTitle As String
        ulFlags As Long
        lpfn As Long
        lParam As Long
        iImage As Long
    End Type
                        
    Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" _
        (lpBrowseInfo As BROWSEINFO) As Long
    Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" _
        (ByVal pidl As Long, ByVal pszPath As String) As Boolean
#End If

Private Const BIF_RETURNONLYFSDIRS = &H1

Sub GetPasta()
    
    Dim bnfo As BROWSEINFO
    Dim sCaminho As String
    Dim lÍndice As Long
    Dim vJanela As Variant
    Dim iPosição As Integer
    Dim sPasta As String
 
    'vJanela foi declarada como Variant porque ela é do tipo
    'Long se o Office for 2007, e LongPtr se o Office for 2010.
 
    'A pasta raiz é o Desktop:
    bnfo.pidlRoot = 0&

    'Título
    bnfo.lpszTitle = "Selecione uma Pasta:"
    
    'Tipo de dado retornado:
    bnfo.ulFlags = &H1

    'Mostra a janela:
    vJanela = SHBrowseForFolder(bnfo)
    
    'Analisa e trata o resultado:
    sCaminho = Space(512)
    lÍndice = SHGetPathFromIDList(ByVal vJanela, ByVal sCaminho)
    If lÍndice Then
        iPosição = InStr(sCaminho, Chr(0))
        sPasta = Left(sCaminho, iPosição - 1)
        Debug.Print sPasta
    Else
        'Nenhuma Pasta foi selecionada
    End If

End Sub

 

0 Likes