VBS Opens the Select File dialog box code (Win7) _vbs

Source: Internet
Author: User
Tags volatile
I'm sure a lot of people have seen "hi, Scripting guy!. "One of the problems above, even if you have not read the original, you should have seen the code copied and pasted."

How do i show the User a dialog box to select a file?

Q: Hi, scripting guy! Is there any way that I can use a script to show the User a dialog box for the user to select a file to use? -bf

Answer: Hello, BF. If you are using Windows 2000, we do not know how to do this, at least not in the operating system. But 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 the user. You can use a script that resembles the following code:


The code I will not copy paste, the original inside there, the Internet is also everywhere. The problem is that this code is only available for Windows XP Systems (Windows 2003 may be OK, but I haven't tested them), and Windows 7 is starting to get popular now. In Vista and Windows 7 systems, the default is not to bring the UserAccounts.CommonDialog component (incidentally, Safrcfiledlg.fileopen and Safrcfiledlg.filesave components are not).

So how do you show the User a dialog box to select a file in a Windows 7 system? The answer is the File selection dialog box in HTML:
Copy Code code 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 _
""
"<title>Browse</title>" & _
""<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>" & _
"
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: 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.