Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

VBScript FAQ


How do I open a common file dialog window? by zaznet
Posted: 15 Jul 04 (Edited 16 Jul 04)

If you have ever wanted to force a user to select a file you have wondered how to use the commonly used

"File Open Dialog" from windows.  Most windows applications use the same dialog window, this is called a

"Common Dialog".  The below FAQ will help answer that question.  Keep in mind that version changes in VB,

VBScript, and Windows may prevent code demonstrated from working in all instances.  This code works for

Windows 2000 and XP at the time of posting.

The first step is to create an Object type variable in VB.

Set ObjFSO = CreateObject("UserAccounts.CommonDialog")

We have created "ObjFSO" to hold our dialog box.  The "UserAccounts.CommonDialog" is the class of object we

are creating.  This is that File Open Dialog we want.  You can change "ObjFSO" to some longer name that

makes more sense for you, it is our variable.

It is important to note that we have not yet opened the dialog!  In order to open the dialog we have to use

a "ShowOpen" property and catch the exit value (return or error code) of the dialog box.  It is as simple

as creating a variable to store that exit value.

InitFSO = ObjFSO.ShowOpen

Once input we've got a new variable "InitFSO" that contains the answer to an important question.  Did our

user select a file?  Returns 0 (the constant false) if no file was selected.  Returns -1 (the constant for

true) if the file was selected.

So now the user has either selected a file or aborted your dialog.  We now need to act on what they have

done.  We need at least two parts.  One result if they did not select a file.  Another result when they do

select a file.  We check the value of "InitFSO" and act accordingly.

If InitFSO = False Then
    Wscript.Echo "Script Error: Please select a file!"
    Wscript.Echo "You selected a file, great!"
End If

Now that we can check on a file selected or not, how do we know what file was selected?  We use the

property Filename against our ObjFSO object variable.

Wscript.Echo "You selected the file: " & ObjFSO.FileName

OK, now on to a more advanced topic.  How to make some changes to the dialog box before it's called!  We

have to set several properties BEFORE we use the ShowOpen property on the object variable.

The dialog has 3 properties we can set before clalign it.  They are the file filter list, the filter index

(what is selected from the filter list), and the default location to look for files.

To create the list, use the Filter property on our object variable.

ObjFSO.Filter = "VBScripts|*.vbs|Text Documents|*.txt|All Files|*.*"

The string is paired and the first part of the pair is displayed in the dialog.  The second part is used to

filter the files.  

If we want All Files to be used by default, we set the FilterIndex property to the 3rd pair in our list.

ObjFSO.FilterIndex = 3

Our final property is the start location and we set the InitialDir property to a string for our path.  This

could be a UNC path ( \\server\shaer\folder\ ) or relative path ( ..\..\path\to\files ).  The intial path

will be the current path when run from a command prompt if this property is not set.

ObjFSO.InitialDir = "c:\myscripts"

Here is a sample complete code of all the functions demonstrated in this FAQ.


Set ObjFSO = CreateObject("UserAccounts.CommonDialog")

ObjFSO.Filter = "VBScripts|*.vbs|Text Documents|*.txt|All Files|*.*"

ObjFSO.FilterIndex = 3

ObjFSO.InitialDir = "c:\myscripts"

InitFSO = ObjFSO.ShowOpen

If InitFSO = False Then
    Wscript.Echo "Script Error: Please select a file!"
    Wscript.Echo "You selected the file: " & ObjFSO.FileName
End If

Back to VBScript FAQ Index
Back to VBScript Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close