Code of the VBS open Select File Dialog Box (Win7)

Source: Internet
Author: User

I believe many people have read "Hi, Scripting Guy !" Even if you haven't read the original text, you should have read the copied and pasted code.

How can I display a dialog box for selecting files to users?

Q: Hi, Scripting Guy! Is there any way for me to use a script to display a dialog box for users to select files? -BF

A: Hello, BF. If you are using Windows 2000, we do not know how to implement this operation. At least this method is not built in the operating system. However, if you are using Windows XP, the situation is different. On Windows XP, you can use the "UserAccounts. CommonDialog" object to display a standard "file open" dialog box to users. You can use a script similar to the following code:

I will not copy or paste the code. The original Article contains the code, and the code is everywhere on the Internet. However, the problem is that this code can only be used in Windows XP (Windows 2003 may also work, but I have not tested it), and now Windows 7 has become popular. In Vista and Windows 7 systems, the UserAccounts. CommonDialog component is not provided by default (by the way, the SAFRCFileDlg. FileOpen and SAFRCFileDlg. FileSave components are also unavailable ).

In Windows 7, how does one display a dialog box for selecting files? The answer is the file selection dialog box in html:Copy codeThe Code is as follows: Function BrowseForFile ()
Dim shell: Set shell = CreateObject ("WScript. Shell ")
Dim fso: Set fso = CreateObject ("Scripting. FileSystemObject ")

Dim tempFolder: Set tempFolder = fso. GetSpecialFolder (2)
Dim tempName: tempName = fso. GetTempName ()
Dim tempFile: Set tempFile = tempFolder. CreateTextFile (tempName & ". hta ")

TempFile. Write _
"<Html> "&_
"<Head> "&_

"<Title> Browse </title> "&_
"</Head> "&_
"<Body> "&_

"<Input type = 'file' id = 'F'/> "&_
"<Script type = 'text/javascript '> "&_
"Var f = document. getElementById ('F ');"&_

"F. click ();"&_
"Var shell = new ActiveXObject ('wscript. shell ');"&_
"Shell. RegWrite ('HKEY _ CURRENT_USER \ Volatile Environment \ MsgResp ', f. value );"&_
"Window. close ();"&_

"</Script> "&_
"</Body> "&_
"</Html>"

TempFile. Close
Shell. Run tempFolder & "\" & tempName & ". hta", 0, True
BrowseForFile = shell. RegRead ("HKEY_CURRENT_USER \ Volatile Environment \ MsgResp ")

Shell. RegDelete "HKEY_CURRENT_USER \ Volatile Environment \ MsgResp"
End Function

'Author: Demon
'Website: http://demon.tw
'Date: 2011/6/2

Path = BrowseForFile ()
If path <> "" Then WScript. Echo path

Original article: http://demon.tw/programming/vbs-open-file-dialog.html

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.