◎ Vbs Script Programming Tutorial 12-use the system dialog box

Source: Internet
Author: User

In the vbs script design, if you can use the system dialog box provided by Windows, you can simplify the ease of use of the script and make the script more user-friendly, which is rarely used. However, vbs does not provide such a function, of course, the method still uses COM objects.

1. safrcfiledlg. filesave object: attributes: Filename-specify the default file name; filetype-

Specify the file extension. openfilesavedlg-display the file storage box method.

2. safrcfiledlg. fileopen object: Filename-default file name attribute; openfileopendlg-Display Method for opening the file box.

3. useraccounts. commondialog object: Filter-extension attributes ("vbs file | *. vbs | all files | *.*");

Filterindex-specified

Initialdir-specify the default folder

Filename-specified file name

Flags-Dialog Box Type

Showopen method:

Very simple. OK. Let's take two simple examples:

Example 1: save an object

Set objdialog = Createobject ("safrcfiledlg. filesave ")

Set objfso = Createobject ("scripting. FileSystemObject ")

Objdialog. filename = "test"

Objdialog. filetype = ". txt"

Intreturn = objdialog. openfilesavedlg

If intreturn then

Objfso. createtextfile (objdialog. filename & objdialog. filetype)

Else

Wscript. Quit

End if

Note: 1. The safrcfiledlg. filesave object only provides a user-friendly interface, and does not save the file itself. You need to use the FSO object to save the file. 2. Use the filetype attribute to specify the default file type. 3. When calling the openfilesavedlg method, it is best to save the returned value to a variable and use it to determine whether the user presses "OK" or "cancel.

Example 2: open a file

Set objfile = Createobject ("safrcfiledlg. fileopen ")

Intret = objfile. openfileopendlg

If intret then

Msgbox "file opened successfully! The file name is: "& objfile. filename

Else

Wscript. Quit

End if

Example 3: complex file opening dialog box

Set objdialog = Createobject ("useraccounts. commondialog ")

Objdialog. Filter = "vbs file | *. vbs"

Objdialog. initialdir = "C :/"

Tfile = objdialog. showopen

If tfile then

Strloadfile = objdialog. filename

Msgbox strloadfile

Else

Wscript. Quit

End if

Note: Add objdialog. Flags = & h020 to the script to see what will happen.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.