Code Snippet: Browse for Folder 
Author Message
 Code Snippet: Browse for Folder

Can anyone offer a "clean" piece of code to browse for a folder name,
without having to browse for a file?

Jeff Hall



Sat, 08 Nov 2003 21:12:00 GMT  
 Code Snippet: Browse for Folder
Hi Jeff,

There are a variety of possible ways. Take a look here for some of them

How to allow the user to browse to and select a folder
http://www.mvps.org/word/FAQs/MacrosVBA/BrowsDialog.htm

--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
Word FAQs at http://www.multilinker.com/wordfaq
Please post any follow-up in the newsgroup. I do not reply to Word questions
by email


Quote:
> Can anyone offer a "clean" piece of code to browse for a folder name,
> without having to browse for a file?

> Jeff Hall



Sat, 08 Nov 2003 21:34:00 GMT  
 Code Snippet: Browse for Folder
Hi,

the following was taken from "Office 2000 VBA Fundamentals" from Microsoft
Press

Attribute VB_Name = "modBrowseFolder"
Option Explicit

'---------------------------------------------------
' WinAPI Declarations
'---------------------------------------------------
Private Declare Sub CoTaskMemFree _
    Lib "ole32.dll" ( _
        ByVal hMem As Long _
    )
Private Declare Function SHBrowseForFolder _
    Lib "shell32" ( _
        lpbi As BrowseInfo _
    ) As Long
Private Declare Function SHGetPathFromIDList _
    Lib "shell32" ( _
        ByVal pidList As Long, _
        ByVal lpBuffer As String _
    ) As Long
Private Declare Function GetActiveWindow _
    Lib "user32" ( _
    ) As Long

'-------------------------------------------------
' User-Defined Types
'-------------------------------------------------
Private Type BrowseInfo
    hwndOwner As Long
    pIDLRoot As Long
    pszDisplayName As String
    lpszTitle As String
    ulFlags As Long
    lpfnCallback As Long
    lParam As Long
    iImage As Long
End Type

'-------------------------------------------------
' Module-level Constants
'-------------------------------------------------
'used for SHBrowseForFolder APIs
Const BIF_RETURNONLYFSDIRS = 1
Const MAX_PATH = 260

Function BrowseForFolder() As String
    Dim iNull As Integer
    Dim lpIDList As Long
    Dim lResult As Long
    Dim sPath As String
    Dim udtBI As BrowseInfo

    With udtBI
        .hwndOwner = GetActiveWindow&
        .lpszTitle = "Choose a folder"
        .ulFlags = BIF_RETURNONLYFSDIRS
    End With

    lpIDList = SHBrowseForFolder(udtBI)
    If lpIDList Then
        sPath = String$(MAX_PATH, 0)
        lResult = _
            SHGetPathFromIDList _
            (lpIDList, sPath)
        Call CoTaskMemFree(lpIDList)
        iNull = InStr(sPath, _
            vbNullChar)
        If iNull Then _
            sPath = Left$(sPath, iNull - 1)
    End If
    BrowseForFolder = sPath
End Function



Quote:
> Can anyone offer a "clean" piece of code to browse for a folder name,
> without having to browse for a file?

> Jeff Hall



Sun, 09 Nov 2003 19:28:34 GMT  
 Code Snippet: Browse for Folder
Is there any way to make this folder browser start off at a given folder,
rather than at the root?

Jeff Hall

Quote:
> the following was taken from "Office 2000 VBA Fundamentals" from Microsoft
> Press

> Attribute VB_Name = "modBrowseFolder"
> Option Explicit

> '---------------------------------------------------
> ' WinAPI Declarations
> '---------------------------------------------------
> Private Declare Sub CoTaskMemFree _
>     Lib "ole32.dll" ( _
>         ByVal hMem As Long _
>     )
> Private Declare Function SHBrowseForFolder _
>     Lib "shell32" ( _
>         lpbi As BrowseInfo _
>     ) As Long
> Private Declare Function SHGetPathFromIDList _
>     Lib "shell32" ( _
>         ByVal pidList As Long, _
>         ByVal lpBuffer As String _
>     ) As Long
> Private Declare Function GetActiveWindow _
>     Lib "user32" ( _
>     ) As Long

> '-------------------------------------------------
> ' User-Defined Types
> '-------------------------------------------------
> Private Type BrowseInfo
>     hwndOwner As Long
>     pIDLRoot As Long
>     pszDisplayName As String
>     lpszTitle As String
>     ulFlags As Long
>     lpfnCallback As Long
>     lParam As Long
>     iImage As Long
> End Type

> '-------------------------------------------------
> ' Module-level Constants
> '-------------------------------------------------
> 'used for SHBrowseForFolder APIs
> Const BIF_RETURNONLYFSDIRS = 1
> Const MAX_PATH = 260

> Function BrowseForFolder() As String
>     Dim iNull As Integer
>     Dim lpIDList As Long
>     Dim lResult As Long
>     Dim sPath As String
>     Dim udtBI As BrowseInfo

>     With udtBI
>         .hwndOwner = GetActiveWindow&
>         .lpszTitle = "Choose a folder"
>         .ulFlags = BIF_RETURNONLYFSDIRS
>     End With

>     lpIDList = SHBrowseForFolder(udtBI)
>     If lpIDList Then
>         sPath = String$(MAX_PATH, 0)
>         lResult = _
>             SHGetPathFromIDList _
>             (lpIDList, sPath)
>         Call CoTaskMemFree(lpIDList)
>         iNull = InStr(sPath, _
>             vbNullChar)
>         If iNull Then _
>             sPath = Left$(sPath, iNull - 1)
>     End If
>     BrowseForFolder = sPath
> End Function



Tue, 25 Nov 2003 01:20:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Code works when run in VB6, gets error when compiled (Browse for Outlook Folder)

2. Code works when run in VB6, gets error when compiled (Browse for Outlook Folder)

3. "Browse For Folder", Things source code

4. Start DIRECTORY (FOLDER) for the Browse Folder function

5. folder browsing - create folder problem

6. instead of using browse for folder, how can u put those virtual folder into a co

7. Specify an initial folder for the Browse for folder dialog

8. Browse for folder : specify root folder ?

9. Code Database Program - Store your code Snippets

10. Code Database Program- Store your code snippets

11. Back to VS.Net beta2, there was a code share plugin featured browsing code from Internet

12. samples code snippets?

 

 
Powered by phpBB® Forum Software