8 out of 13 rated this helpful - Rate this topic

Shell.BrowseForFolder method

Applies to: desktop apps only

Creates a dialog box that enables the user to select a folder and then returns the selected folder's Folder object.

Syntax

retVal = Shell.BrowseForFolder(
  Hwnd,
  sTitle,
  iOptions,
  [ vRootFolder ]
)

Parameters

Hwnd [in]

Type: Integer

The handle to the parent window of the dialog box. This value can be zero.

sTitle [in]

Type: BSTR

A String value that represents the title displayed inside the Browse dialog box.

iOptions [in]

Type: Integer

An Integer value that contains the options for the method. This can be zero or a combination of the values listed under the ulFlags member of the BROWSEINFO structure.

vRootFolder [in, optional]

Type: Variant

The root folder to use in the dialog box. The user cannot browse higher in the tree than this folder. If this value is not specified, the root folder used in the dialog box is the desktop. This value can be a string that specifies the path of the folder or one of the ShellSpecialFolderConstants values. Note that the constant names found in ShellSpecialFolderConstants are available in Visual Basic, but not in VBScript or JScript. In those cases, the numeric values must be used in their place.

Return value

Type: FOLDER**

An object reference to the selected folder's Folder object.

Examples

The following example uses BrowseForFolder to display a browse window titled "Example" rooted at the Windows folder. Proper usage is shown for JScript, VBScript, and Visual Basic.

JScript:


<script language="JScript">
    function fnShellBrowseForFolderJ()
    {
        var objShell = new ActiveXObject("shell.application");
        var ssfWINDOWS = 36;
        var objFolder;
        
        objFolder = objshell.BrowseForFolder(0, "Example", 0, ssfWINDOWS);
        if (objFolder != null)
        {
            // Add code here.
        }
    }
</script>


VBScript:

<script language="VBScript">
    function fnShellBrowseForFolderVB()
        dim objShell
        dim ssfWINDOWS
        dim objFolder
        
        ssfWINDOWS = 36
        set objShell = CreateObject("shell.application")
            set objFolder = objshell.BrowseForFolder(0, "Example", 0, ssfWINDOWS)
                if (not objFolder is nothing) then
                    'Add code here.
                end if
            set objFolder = nothing
        set objShell = nothing
    end function
 </script>

Visual Basic:

Private Sub fnShellBrowseForFolderVB()
    Dim objShell   As Shell
    Dim ssfWINDOWS As Long
    Dim objFolder  As Folder
    
    ssfWINDOWS = 36
    Set objShell = New Shell
        Set objFolder = objshell.BrowseForFolder(0, "Example", 0, ssfWINDOWS)
            If (Not objFolder Is Nothing) Then
                'Add code here
            End If
        Set objFolder = Nothing
    Set objShell = Nothing
End Sub

Requirements

Minimum supported client

Windows 2000 Professional, Windows XP

Minimum supported server

Windows 2000 Server

Header

Shldisp.h

IDL

Shldisp.idl

DLL

Shell32.dll (version 4.71 or later)

 

 

Send comments about this topic to Microsoft

Build date: 3/7/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Typo
s changes from big to small in objShell in the examples above ie dim objShell and set objFolder = objshell.Brows....
VB Coding Issues (Browse for Folder)
I took the original VB code provided on MSDN and modified as follows below. When I run my event, cmdSelectFolder_Click from my MS Access form and attempt to display via (MsgBox), the selected folder's name, I get a runtime error 13 "Type mismatch" error message. I will note that I'm able to see the Folder dialog box and can browse and select whatever folder I like but the error message pops up before MsgBox can display the .Name property. Any help would be greatly appreciated. thanks. Private Sub cmdSelectFolder_Click() Dim objShell As Shell Dim objFolder As folder Set objShell = New Shell Set objFolder = objShell.BrowseForFolder(0, "Select a Folder", 0, '"C:\Projects") MsgBox objFolder.Name Set objFolder = Nothing Set objShell = Nothing End Sub
ShowOpenFolder Dialog with Shell.Application

'############################# ShowOpenFolder #################################
'This function creates OpenFolder Dialog box in VbScript
'Designed by Rajendra Khope


Function ShowOpenFolder()
 Const MY_COMPUTER = &H11&
 Const WINDOW_HANDLE = 0
 Const OPTIONS = 0
 Dim objShell
 Dim objFolder
 Dim objFolderItem
 Dim strPath
 Dim objPath
 
'Refer this Link http://msdn.microsoft.com/en-us/library/bb774085(VS.85).aspx

 Set objShell = CreateObject("Shell.Application")
 Set objFolder = objShell.Namespace(MY_COMPUTER)
 Set objFolderItem = objFolder.Self
 strPath = objFolderItem.Path

'This for all - http://msdn.microsoft.com/en-us/library/ff521729(v=VS.85).aspx
 Set objShell = CreateObject("Shell.Application")
 Set objFolder = objShell.BrowseForFolder _
 (WINDOW_HANDLE, "Select folder to Save File:", OPTIONS, strPath) 
      
 If objFolder Is Nothing Then
  ShowOpenFolder="C:"
  Exit Function 
 End If
 Set objFolderItem = objFolder.Self
 objPath = objFolderItem.Path
 ShowOpenFolder = objPath
End Function
'######################################################################

BrowseForFolder Sample Using PowerShell
# Browse-Folder.ps1
# MSDN Sample using PowerShell
# Thomas Lee - tfl@psp.co.uk

# First get shell object
$Shell = new-object -com Shell.Application

# Now Open up a browse window
$ssfWINDOWS = 36
$objFolder=$Shell.BrowseForFolder(0, "PowerShell Sample", 0, $ssfWINDOWS)

# display the selected folder
$objFolder

# Count the number of items in the selected folder
"This folder contains {0} items" -f ($objfolder.items()).count

This sample produces the following output on my ssytem, when the C:\Windows folder is selected:
PSH [D:\foo]: .\browse-folders.ps1

Title : WINDOWS
Application : System.__ComObject
Parent :
ParentFolder : System.__ComObject
Self : System.__ComObject
OfflineStatus : -1
HaveToShowWebViewBarricade : True
ShowWebViewBarricade : True

This folder contains 193 items

Help!!!
In my page show "Access denied"... why ???

[tfl - 29/6/09] - You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn. You are much more likely get a quick response using the forums than through the Community Content.

My guess is that the folder you are trying to get to is not available to you - just tested it (using PowerShell) and it works fine!