Astahost.com   Mar 20, 2010
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Scripting > VBScript

Open File Dialog Box - How To Browse for Files

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Scripting > VBScript

Open File Dialog Box - How To Browse for Files

mastercomputers
I am in the process of writing a VBScript to grab a text file and rewrite it into an XML formatted file and decided that for each part that I'm working on I would drop off a small snippet of my code that may or may not be handy for people.

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
CODE
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:

CODE
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:

CODE
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:

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.

CODE
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.


Cheers,


MC

 

 

 


Comment/Reply (w/o sign-up)

(G)krr
thanks
Open File Dialog Box

This code really helped me a lot. Thanks 




Comment/Reply (w/o sign-up)

(G)SelTech
Great help!
Open File Dialog Box

Awesome! Now I can have my users browse rather than type! Thanks!

What I would like to know is, how do you know and where is a reference as to what object to create for a specific thing in your script? How did you know and where did you get a reference that by creating an object using UserAccounts.CommonDialog you would accomplish this? I've never been formally trained on vbscript but just get by through creativity using examples, but I've never been able to find anywhere a reference on what objects I require to create in order to accomplish something I need (I.E. UserAccounts.CommonDialog, Scripting.FileSystemObject, Wscript.Shell, etc) 

-reply by SelTech


Comment/Reply (w/o sign-up)

mastercomputers
All the information on objects you can use for VBScript can be found on the MSDN website.

However this information could be written for C/C++, VB or other languages, you just got to know how to write it for the language you want to use, which usually isn't too different from the examples they show you.


Cheers,


MC

Comment/Reply (w/o sign-up)

(G)Bill
How to enable multi-select?
Open File Dialog Box

Thanks for the code. It worked in my VBA environment. My IDE has auto-complete which is spoiling me. Unfortunately it does not work on this objDialog object. Either I need the correct type or API installed.

What I want to know is the attribute to set to enable multiple file selection and then how to get my array of Files once boolResult == true.

Regards,

Bill

-reply by Bill

Comment/Reply (w/o sign-up)

(G)Bill
How do I enable multi-selection?
Open File Dialog Box

Thanks for the code. It worked in my VBA environment. My IDE has Auto-complete which is spoiling me. Unfortunately it does not work on This objDialog object. Either I need the correct type or API installed.

What I want to know is the attribute to set to enable multiple file Selection and then how to get my array of Files once boolResult == true.

Regards,

 Bill

-reply by Bill

Comment/Reply (w/o sign-up)

(G)AccessPro

Great !

Here is the code to use with VBA Access :

Function testFile()
Dim objDialog, boolResult

Set objDialog = CreateObject("UserAccounts.CommonDialog")

objDialog.Filter = "Tous les fichiers|*.*"
ObjDialog.FilterIndex = 1

boolResult = objDialog.ShowOpen

If boolResult = 0 Then
    Exit Function
Else
    MsgBox "You chose: " & objDialog.FileName
End If
End Function 

-reply by AccessPro


Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : File Dialog Browse Files


    Looking for Open, File, Dialog, Box



See Also,

*SIMILAR VIDEOS*
Searching Video's for Open, File, Dialog, Box
advertisement




Open File Dialog Box - How To Browse for Files

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com



Creative Commons License