Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-10-12
2
Medium Priority
?
1,010 Views
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      = ... 
' ... 
 
  oCDlg.ShowOpen 
 
  doFileOpenDialog = oCDlg.Filename  ' FSpec 
 
End Sub

Open in new window

0
Comment
Question by:Christopher Schene
2 Comments
 
LVL 3

Accepted Solution

by:
DrunkenELF earned 1000 total points
ID: 22698337
Dear Cschene,

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

Cheers,
D-ELF
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
Else
    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
Else
    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
    Else
        IsValue = True
    End If
    On Error GoTo 0
End Function
 
'*** End

Open in new window

0
 

Author Comment

by:Christopher Schene
ID: 22698569
thanks for the fast response....it works well!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This video teaches viewers about errors in exception handling.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

879 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