Jump to content



Welcome to AstaHost - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!

Toggle shoutbox Shoutbox Open the Shoutbox in a popup

@  yordan : (19 June 2013 - 02:28 PM) Long Life To Asta New Era
@  agyat : (19 June 2013 - 01:58 PM) New Era Start At Asta Or Asta Start In New Era. :unsure:
@  yordan : (16 June 2013 - 05:41 PM) You're Welcome, Agyat!
@  agyat : (16 June 2013 - 07:38 AM) Thanks Yordan...
@  velma : (16 June 2013 - 12:06 AM) I Have Asked Opa To Check For A Backup.. He'll Let Me Know Soon :)
@  velma : (16 June 2013 - 12:05 AM) T_T It Seems That Someone Has Deleted That Topic Since I Found The Url Of The Topic But It Gives Me An Error
@  yordan : (15 June 2013 - 10:31 PM) @velma : It's A Tuto On How To Create A Login Program.
@  yordan : (15 June 2013 - 10:31 PM) Happy Birthday To Youuuuuu Agyat!
@  yordan : (15 June 2013 - 10:31 PM) Ba$
@  agyat : (15 June 2013 - 04:41 PM) :(
@  agyat : (15 June 2013 - 04:41 PM) Where The Hall I Were? 15Th Is Almost At End And No-One Wished Me "happy Birthday"!!!
@  velma : (14 June 2013 - 10:39 AM) Which Tutorial Is He Searching For?
@  velma : (14 June 2013 - 10:38 AM) Which Tutorial Is He Searching For?
@  yordan : (14 June 2013 - 07:47 AM) Ok, Have A Look Tomorrow.
@  yordan : (13 June 2013 - 03:19 PM) @velma, Can You Have A Look At Feelay's Problem? Seems That His Tutorial Is Not Searchable Today.
@  Feelay : (13 June 2013 - 08:11 AM) Oh, Haha
@  velma : (12 June 2013 - 05:39 PM) T_T Lately My Levels Of Procrastination..... **sigh**
@  velma : (12 June 2013 - 05:38 PM) I'll Do It Later
@  velma : (12 June 2013 - 05:38 PM) Procrastinators.. People Who Keep Saying "i'll Do This In A Bit"
@  Feelay : (12 June 2013 - 02:05 PM) Deal Punishments To What?

Photo
- - - - -

Open File Dialog Box


7 replies to this topic

#1 mastercomputers

mastercomputers

    Making IT Happen

  • Members
  • 770 posts
  • Gender:Male
  • Location:Auckland, New Zealand
  • Interests:There's not a lot I'm not interested in, knowledge is power, without it, I'd be sitting in the dark.
  • myCENTs:42.89

Posted 08 July 2009 - 06:47 AM

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


Cheers,


MC

#2 Guest_(G)krr_*

Guest_(G)krr_*
  • Guests

Posted 30 November 2009 - 02:15 PM

thanksOpen File Dialog Box

This code really helped me a lot. Thanks 



#3 Guest_(G)SelTech_*

Guest_(G)SelTech_*
  • Guests

Posted 02 December 2009 - 01:24 AM

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

#4 mastercomputers

mastercomputers

    Making IT Happen

  • Members
  • 770 posts
  • Gender:Male
  • Location:Auckland, New Zealand
  • Interests:There's not a lot I'm not interested in, knowledge is power, without it, I'd be sitting in the dark.
  • myCENTs:42.89

Posted 21 December 2009 - 08:47 AM

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

#5 Guest_(G)Bill_*

Guest_(G)Bill_*
  • Guests

Posted 30 December 2009 - 04:34 PM

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

#6 Guest_(G)Bill_*

Guest_(G)Bill_*
  • Guests

Posted 30 December 2009 - 04:38 PM

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

#7 Guest_(G)AccessPro_*

Guest_(G)AccessPro_*
  • Guests

Posted 15 December 2009 - 03:38 PM

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

#8 Guest_(G)jasvant_*

Guest_(G)jasvant_*
  • Guests

Posted 04 August 2011 - 07:45 AM

'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





Reply to this topic



  


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users