I accept cookies

This website uses cookies to ensure you get the best experience on our website More info

VBScript Scripting Techniques > User Interaction > File Open Dialog

File Open Dialog

UserAccounts.CommonDialog
VBScript Code:
WScript.Echo "Selected file: " & GetFileName( "C:\", "" )
WScript.Echo "Selected file: " & GetFileName( "", "Text files|*.txt" )
WScript.Echo "Selected file: " & GetFileName( "", "MS Office documents|*.doc;*.xls;*.pps" )
WScript.Echo "Selected file: " & GetFileName( "C:\WINDOWS", "Bitmaps|*.bmp" )

Function GetFileName( myDir, myFilter )
' This function opens a File Open Dialog and returns the
' fully qualified path of the selected file as a string.
'
' Arguments:
' myDir is the initial directory; if no directory is
' specified "My Documents" is used;
' NOTE: this default requires the WScript.Shell
' object, and works only in WSH, not in HTAs!
' myFilter is the file type filter; format "File type description|*.ext"
' ALL arguments MUST get A value (use "" for defaults), OR otherwise you must
' use "On Error Resume Next" to prevent error messages.
'
' Dependencies:
' Requires NUSRMGRLib (nusrmgr.cpl), available in Windows XP and later.
' To use the default "My Documents" WScript.Shell is used, which isn't
' available in HTAs.
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com

' Standard housekeeping
    Dim objDialog

' Create a dialog object
    Set objDialog = CreateObject( "UserAccounts.CommonDialog" )

' Check arguments and use defaults when necessary
    If myDir = "" Then
        ' Default initial folder is "My Documents"
        objDialog.InitialDir = CreateObject( "WScript.Shell" ).SpecialFolders( "MyDocuments" )
    Else
        ' Use the specified initial folder
        objDialog.InitialDir = myDir
    End If
    If myFilter = "" Then
        ' Default file filter is "All files"
        objDialog.Filter = "All files|*.*"
    Else
        ' Use the specified file filter
        objDialog.Filter = myFilter
    End If

' Open the dialog and return the selected file name
    If objDialog.ShowOpen Then
        GetFileName = objDialog.FileName
    Else
        GetFileName = ""
    End If
End Function

 
Requirements:
Windows version: Windows XP
Network: N/A
Client software: N/A
Script Engine: any (WSH if using default for directory)
Additional options: objDialog.Filter = "MS Office files|*.doc;*.xls;*.pps|Text files|*.txt|All files|*.*"
objDialog.FilterIndex = 1 'MS Office files
objDialog.FilterIndex = 2 'Text files
objDialog.FilterIndex = 3 'All files

objDialog.Flags = 1 'Check "Open file as read-only" checkbox in dialog
objDialog.Flags = 0 'Restore default read-write mode

Summarized: Works in Windows XP only.
If used in HTAs, the initial directory must be specified.
Doesn't work in any other Windows version.
 
 
[Back to the top of this page]
 
SAFRCFileDlg.FileOpen
VBScript Code:
Set objDialog = CreateObject( "SAFRCFileDlg.FileOpen" )

' Note: The dialog will be opened without any file name or
'       type filter, and in the "current" directory, e.g. as
'       remembered from the last "SAFRCFileDlg.FileOpen" or
'       "SAFRCFileDlg.FileSave" dialog!
If objDialog.OpenFileOpenDlg Then
    WScript.Echo "objDialog.FileName = " & objDialog.FileName
End If

 
Requirements:
Windows version: Windows XP, Server 2003
Network: N/A
Client software: N/A
Script Engine: any
Summarized: Works in all Windows XP versions and in Server 2003.
Doesn't work in Windows 95, 98, ME, NT 4, 2000 or 7, not sure about Vista.
 
 
[Back to the top of this page]
 
InternetExplorer.Application
VBScript Code:
Option Explicit

WScript.Echo "Selected file: " & ChooseFile( )

Function ChooseFile( )
' Select File dialog based on a script by Mayayana
' Known issues:
' * Tree view always opens Desktop folder
' * In Win7/IE8 only the file NAME is returned correctly, the path returned will always be C:\fakepath\
' * If a shortcut to a file is selected, the name of that FILE will be returned, not the shortcut's
    On Error Resume Next
    Dim objIE, strSelected
    ChooseFile = ""
    Set objIE = CreateObject( "InternetExplorer.Application" )
    objIE.visible = False
    objIE.Navigate( "about:blank" )
    Do Until objIE.ReadyState = 4
    Loop
    objIE.Document.Write "<HTML><BODY><INPUT ID=""FileSelect"" NAME=""FileSelect"" TYPE=""file""><BODY></HTML>"
    With objIE.Document.all.FileSelect
        .focus
        .click
        strSelected = .value
    End With
    objIE.Quit
    Set objIE = Nothing
    ChooseFile = strSelected
End Function

 
Requirements:
Windows version: any
Network: N/A
Client software: Internet Explorer
Script Engine: any
Summarized: Works in all Windows versions.
 
 
[Back to the top of this page]
 
WScript.Shell.Exec MSHTA
VBScript Code:
Option Explicit

Dim strFile

strFile = SelectFile( )

If strFile = "" Then 
    WScript.Echo "No file selected."
Else
    WScript.Echo """" & strFile & """"
End If

Function SelectFile( )
    ' File Browser via HTA
    ' Author:   Rudi Degrande, modifications by Denis St-Pierre and Rob van der Woude
    ' Features: Works in Windows Vista and up (Should also work in XP).
    '           Fairly fast.
    '           All native code/controls (No 3rd party DLL/ XP DLL).
    ' Caveats:  Cannot define default starting folder.
    '           Uses last folder used with MSHTA.EXE stored in Binary in [HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32].
    '           Dialog title says "Choose file to upload".
    ' Source:   https://social.technet.microsoft.com/Forums/scriptcenter/en-US/a3b358e8-15ae-4ba3-bca5-ec349df65ef6/windows7-vbscript-open-file-dialog-box-fakepath?forum=ITCG

Dim objExec, strMSHTA, wshShell

SelectFile = ""

' For use in HTAs as well as "plain" VBScript:
    strMSHTA = "mshta.exe ""about:" & "<" & "input type=file id=FILE>" _
             & "<" & "script>FILE.click();new ActiveXObject('Scripting.FileSystemObject')" _
             & ".GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);" & "<" & "/script>"""
    ' For use in "plain" VBScript only:
    ' strMSHTA = "mshta.exe ""about:<input type=file id=FILE>" _
    '          & "<script>FILE.click();new ActiveXObject('Scripting.FileSystemObject')" _
    '          & ".GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);</script>"""

Set wshShell = CreateObject( "WScript.Shell" )
    Set objExec = wshShell.Exec( strMSHTA )

SelectFile = objExec.StdOut.ReadLine( )

Set objExec = Nothing
    Set wshShell = Nothing
End Function

 
Requirements:
Windows version: Windows XP and later versions
Network: N/A
Client software: MSHTA.EXE (native in Windows)
Script Engine: any
Summarized: Works in Windows XP, Vista, Windows 7, Windows 8, Windows 8.1.
 
 
[Back to the top of this page]
 

page last uploaded: 2016-09-19, 14:58

VBScript Scripting Techniques: File Open Dialog http://www.robvanderwoude.com/vbstech_ui_fileopen.php的更多相关文章

  1. VC++ chap12 file

    file operation _______C语言对文件操作的支持 fopen accepts paths that are valid on the file system at the point ...

  2. ux.plup.File plupload 集成 ux.plup.FileLis 批量上传预览

    //plupload 集成 Ext.define('ux.plup.File', { extend: 'Ext.form.field.Text', xtype: 'plupFile', alias: ...

  3. html 5 drag and drop upload file

    compatible: chrome firefox ie 11 , not supported demo: http://demo.tutorialzine.com/2011/09/html5-fi ...

  4. InstallShield 创建自己的Dialog

    1.在"User Interface"-"Dialogs"下,在All Dialogs右击"New Dialogs-"创建自己的Dialog ...

  5. Test Scenarios for image upload functionality (also applicable for other file upload functionality)

    1 check for uploaded image path2 check image upload and change functionality3 check image upload fun ...

  6. RoR unobtrusive scripting adapter--UJS(一些Javascript的语法糖)

    Learn how the new Rails UJS library works and compares with the old version of jquery_ujs that it re ...

  7. [javaSE] GUI(对话框Dialog)

    对话框不能单独存在,依赖于窗体,有显示标题,有模式 获取Dialog对象,new出来,构造参数:Frame对象,String的标题,模式 窗体内部的内容,Label对象,Button对象,调用Dial ...

  8. dialog - 从 shell 显示对话框

    总览 (SYNOPSIS) dialog --clear dialog --create-rc file dialog --print-maxsize dialog common-options bo ...

  9. 【转载】怎样使用ZEMAX导出高质量的图像动画

    Zemax 导出高质量图片与动画 (2013-08-13 11:01:51) http://blog.sina.com.cn/s/blog_628dd2bc0101dycu.html 转载▼ How ...

随机推荐

  1. Python3中的运算符

    一.Python3中的运算符 强调这是Python3中的运算符 +    加法 -     减法 *     乘法 /     除法 //    整除,只要整数部分 **   幂运算 %   取余数 ...

  2. Ubuntu16.04通过GPT挂载硬盘

    一般而言,服务器上挂载的硬盘都是比较大的,传统的对硬盘进行分区需要在终端敲sudo fdisk进行操作,但是, 当挂载的硬盘的容量大于2T的时候,是无法通过sudo fdisk进行挂载的,这个时候必须 ...

  3. NOI-1.1-08-字符三角形

    08:字符三角形 总时间限制:  1000ms 内存限制:  65536kB 描述 给定一个字符,用它构造一个底边长5个字符,高3个字符的等腰字符三角形. 输入 输入只有一行, 包含一个字符. 输出 ...

  4. Maxscale-在第一个节点的配置

    [maxscale]threads=4 ##### Write Service, need to set address[server1]type=serveraddress=172.16.50.36 ...

  5. 牛客G-指纹锁【一题三解】

    链接:https://www.nowcoder.com/acm/contest/136/G来源:牛客网 题目描述     HA实验有一套非常严密的安全保障体系,在HA实验基地的大门,有一个指纹锁.   ...

  6. 【挑战赛16A】【取石子】【组合数学】

    链接:https://www.nowcoder.com/acm/contest/113/A 来源:牛客网 取石子时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 262144K,其他语言5 ...

  7. 《DSP using MATLAB》Problem 5.31

    第3小题: 代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Out ...

  8. word怎么在方框中打对号

    最快最简单的方法,是在word里输入一个大写的R,然后选中并将字体改为wingdings2,至于那个带叉号的方框图形,可以输入大写字母T并将字体设置为windings2

  9. Singer 学习五 docker 运行说明

    介绍过一个工具knots ,方便Singer 可视化开发的工具,但是默认这个工具包含的tap 以及target 比较少(可以自己扩展) 同时这个工具就是基于docker 运行的 docker 运行的几 ...

  10. 修改VS2017模板文件,添加文件头部自定义注释

    找到Class.cs文件 找到VS2017安装目录下面的Class.cs文件,一般在C盘或者D盘 模块文件位置: 接口模版:C:\Program Files (x86)\Microsoft Visua ...