Jump to content
xisto Community
Sign in to follow this  
iGuest

Open File Dialog Box How To Browse for Files

Recommended Posts

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

Option ExplicitDim objDialog, boolResultSet objDialog = CreateObject("UserAccounts.CommonDialog")objDialog.Filter = "Text File|*.txt"objDialog.FilterIndex = 1boolResult = objDialog.ShowOpenIf boolResult = 0 Then[tab][/tab]WScript.Quit 'Cancel was hitElse[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 objDialogDim 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 hitElse[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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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 FunctionElse   MsgBox "You chose: " & objDialog.FileNameEnd IfEnd Function 

-reply by AccessPro

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
How do I enable multi-selection?Open File Dialog Box

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

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

Regards,

 Bill

-reply by Bill

Share this post


Link to post
Share on other sites

'CMD OPENPrivate Sub cmdopen_Click()Dim clbasic As New a.Clbasic Dim clsbasic As New a.Clbasic cdbopen.CancelError = True cdbopen.Flags = cdlOFNHideReadOnly cdbopen.Filter = "All Files (*.*)|*.*|Text Files" & _ "(*.Txt)|*.Txt|Gatch Files (*.Bat)|*.Bat|" cdbopen.FilterIndex = 2 cdbopen.ShowOpen Call clsbasic.OpenFileTxt(Text1, cdbopen.FileName) End Sub

-reply by jasvant

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.