Using MSComDlg.CommonDialog to make a Directory Browser in VBScript

Posted on 2006-04-11
Last Modified: 2012-06-27

I am having a problem with VBScript and the common dialog (MSComDlg.CommonDialog).
What I am trying to do is build a dialog that allows me to select a target
directory. I am trying to copy this functionality from the Microsoft Word 2003
Dialog where a user can change the default path for documents. Tools - Options -
File Locations - Documents - Modify. I know about the function  Shell.Application
- BrowseForFolder but it does look as good as the Office Dialog I want to simulate.

So really in the code below I want to make it only browse directories and
remove the file filter (Files of type:) - how can this be done. As well
I want to make it only display directories and not list any files.

Enough waffle - here is the code.



Public Const ofnAllowMultiSelect = &H200
Public Const ofnCreatePrompt = &H2000
Public Const ofnFileMustExist = &H1000
Public Const ofnHideReadOnly = &H4
Public Const ofnNoNetworkButton = &H20000
Public Const ofnNoReadOnlyReturn = &H8000
Public Const ofnNoTestfileCreate = &H10000
Public Const ofnNoValidate = &H100
Public Const ofnOverwritePrompt = &H2
Public Const ofnPathMustExist = &H800
Public Const ofnReadOnly = &H1
Public Const ofnShareAware = &H4000
Public Const ofnExplorer = &H80000
Public Const ofnNochangedir = &H8

Dim oComDlg
Dim path_name
Set oComDlg = CreateObject("MSComDlg.CommonDialog")

path_name = ""
With oComDlg
            .DialogTitle = "Please select Target Directory"
            .InitDir = "C:\"
            .Filter = ""
            .FilterIndex = 0
            .MaxFileSize = 260
            .FileName = "*.*"
            .Flags = ofnPathMustExist Or ofnHideReadOnly Or ofnExplorer Or ofnNoValidate Or ofnNochangedir
End With                        
path_name = Left(oComDlg.filename,InStrRev(oComDlg.filename,"\"))

If path_name = "" Then
      MsgBox "User Chose Cancel"
      wscript.quit 0
End If
msgbox "Path Name" & path_name

Question by:whorsfall
    LVL 14

    Expert Comment

    Unfortunately, the Common Dialog component *cannot* be used to browse for folders.


      Nayer Naguib

    Author Comment


    Thanks for your answer. How is it Microsoft are able to do this with Office 2003 - it looks like a common dialog to me. If you try it you can see they can achieve the result - try changing any of the File locations win Word 2003 and you will see what I mean.


    LVL 14

    Expert Comment

    The dialog that appears when you use Microsoft Office 2003 is *not* the MS Common Dialog. I think MS Office applications inherit from the Common Dialog component, and add some extra functionality. If you take a closer look at the dialog box that appears when you want to modify file locations in Word 2003, you will see a "Back" button that stores the browsing history, a "Search the Web" button, and a "Tools" drop down menu.


      Nayer Naguib
    LVL 14

    Accepted Solution

    Take a look at the following discussions:

    The first says that you cannot select folders using the Common Dialog, and the second says that the MS Office 2003 dialog boxes are subclassed from the Common Dialog.


      Nayer Naguib

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Suggested Solutions

    Title # Comments Views Activity
    static class 3 44
    modThree challenge 4 51
    VBA color chart bars 12 50
    SPLUNK REST  API call to Splunk to create and index? 2 41
    This article is filled with multiple code samples and explanations for mathematical calculations. They are as follows: 1. General tips 2. Quadratic formula 3. Object collision 4. Projectile path General Tips       Here are some of my tips f…
    If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    761 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

    11 Experts available now in Live!

    Get 1:1 Help Now