How do I browse to and choose a directory in VBscript using a File dialog?

Posted on 2008-10-12
Last Modified: 2008-10-12
I have the following code attached that will browse and choose a file in VBscript.

Instead of choosing a file, I want to choose a directory: How do  I do that?

Function  doFileOpenDialog() 

  Dim oCDlg 

  Dim sTmp 

  Set oCDlg = CreateObject( "MSComDlg.CommonDialog" ) 

  If oCDlg Is Nothing Then 

     MsgBox "CreateObject( 'MSComDlg.CommonDialog' ) failed." 

     Exit Sub 

  End If 

  oCDlg.MaxFileSize = 10000 

  oCDlg.DialogTitle = "Choose directory"

' Setting other oCDlg.Properties 

' oCDlg.Filter      = ... 

' ... 


  doFileOpenDialog = oCDlg.Filename  ' FSpec 

End Sub

Open in new window

Question by:cschene

Accepted Solution

DrunkenELF earned 250 total points
ID: 22698337
Dear Cschene,

C he attached Code snippet using the shell dialog box to select a folder

Option Explicit

' Flags for the options parameter

Const BIF_returnonlyfsdirs   = &H0001

Const BIF_dontgobelowdomain  = &H0002

Const BIF_statustext         = &H0004

Const BIF_returnfsancestors  = &H0008

Const BIF_editbox            = &H0010

Const BIF_validate           = &H0020

Const BIF_browseforcomputer  = &H1000

Const BIF_browseforprinter   = &H2000

Const BIF_browseincludefiles = &H4000

Dim wsh, objDlg, objF

' Get Application object of the Windows shell.

Set objDlg = WScript.CreateObject("Shell.Application")

' Use the BrowseForFolder method.

' For instance: Set objF = objDlg.BrowseForFolder _

'     (&H0, "Select the folder to copy", &H10, "C:\Born")

Set objF = objDlg.BrowseForFolder (&H0, _

    "Select the folder to copy", _

    BIF_editbox + BIF_returnonlyfsdirs)

' Here we use the first method to detect the result.

If IsValue(objF) Then 

    MsgBox "Selected folder: " & objF.Title


    MsgBox "Canceled"

End If

' Here we use TypeName to detect the result.

If InStr(1, TypeName(objF), "Folder") > 0 Then

    MsgBox "Selected folder: " & objF.Title


    MsgBox "Canceled"

End If

Function IsValue(obj)

    ' Check whether the value has been returned.

    Dim tmp

    On Error Resume Next

    tmp = " " & obj

    If Err <> 0 Then

        IsValue = False


        IsValue = True

    End If

    On Error GoTo 0

End Function

'*** End

Open in new window


Author Comment

ID: 22698569
thanks for the fast works well!

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now