Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I open a common file dialog window?

Applications

How do I open a common file dialog window?

by  zaznet  Posted    (Edited  )
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.Quit
Else
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.

Code:
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.Quit
Else
    Wscript.Echo "You selected the file: " & ObjFSO.FileName
End If
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top