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