I know not many people use VBScript, but it's always handy to know, especially if you just want to create simple solutions using what your Operating System gives you.
Note: These snippets have only been tested on Windows XP which is the platform I'm developing for, it may or may not work on earlier or newer operating systems.
So the first thing I wanted to share is a simple solution to show you how you can create an Open File Dialog Box.
Save this file as OpenDialog.vbs
Option Explicit Dim objDialog, boolResult Set objDialog = CreateObject("UserAccounts.CommonDialog") objDialog.Filter = "Text File|*.txt" objDialog.FilterIndex = 1 boolResult = objDialog.ShowOpen If boolResult = 0 Then [tab][/tab]WScript.Quit 'Cancel was hit Else [tab][/tab]WScript.Echo("You chose: " & objDialog.FileName) End If
This is all we need for this simple script. There's a few extra things I have added, just so I can explain what they do, even though they are not necessary to our script. I will now explain the script:
Option Explicit turns on Strict Mode which helps insure that whatever variables/objects/etc you use are declared before you use them. This prevents variables being created on the fly, which is what would happen without Option Explicit and using Dim to define our variables.
Dim objDialog, boolResult defines two variables that I want to use, objDialog to hold our dialog object and boolResult to hold our returned results on selection made from the dialog box, it only holds true (1) or false (0). They way I defined these variables is quite lazy, and if you are pedantic, it is ok to do:
Dim objDialog Dim boolResult
Which is how I normally write my code, as it helps with readability, especially if you're going to define a large number of variables.
Set objDialog = CreateObject("UserAccounts.CommonDialog"). There's two things to know about this, first CreateObject is a function provided by our scripting language, it creates an object from UserAccounts.CommonDialog. This object is then stored in objDialog using Set and now allows objDialog to be referenced as the newly created/initialised object.
objDialog.Filter = "Text File|*.txt" is what most people want to know more about, the Filter is a property provided by UserAccounts.CommonDialog that allows us to limit what we want our users to select from. For example, say I wanted to show only movie format files, I could do:
objDialog.Filter = "Video Files|*.avi;*.mp4;*.mov"
"Video Files" is what I want to show our user in the Files of Type drop down, separated by the | (bar) then wildcard . (dot) file extension. In which I separate the files using a semi-colon to add more extensions to show in our display.
objDialog.FilterIndex = 1 is code that I didn't need to have, as it is set as 1 by default. The reason I added it is to show you another feature that people would use. To explain it better I'll provide code:
objDialog.Filter = "Text Files|*.txt;*.csv|Word Files|*.doc;*.dot|All Files|*.*" objDialog.FilterIndex = 2
The above code has three types of files we could show and also allow our users to select in the drop down menu for Files of Type, Text Files, Word Files or All Files. The FilterIndex, picks which one to use as default, 2 would select Word Files as default. Text Files = 1, Word Files = 2 and All Files = 3.
boolResult = objDialog.ShowOpen stores our return value from the method ShowOpen which returns 0 if false (usually when cancel is hit) or 1 if a file was selected.
If boolResult = 0 Then [tab][/tab]WScript.Quit 'Cancel was hit Else [tab][/tab]WScript.Echo("You chose: " & objDialog.FileName) End If
This code above is what to do with our result. If it's false, e.g. cancel was hit. Then we just quit our script. Otherwise we will display a message box saying "You chose: " and the full path and extension of the file you chose.
That's it for our simple Open File Dialog Box.