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. Java中的容器 I————浅谈List

    一.List接口的继承关系 List接口是Collection接口的子接口,而ArrayList和LinkedList以及Vector是其实现类. List的特点是可以将元素维护在特定的序列中,可以再 ...

  2. python linecache模块读取文件的方法

    转自: python linecache模块读取文件 在Python中,有个好用的模块linecache,该模块允许从任何文件里得到任何的行,并且使用缓存进行优化,常见的情况是从单个文件读取多行. l ...

  3. aliyun服务器对象存储oss

    aliyun OSS 使用简单.方便. 官方网址 aliyun.com 首先通过aliyun管理控制台申请OSS服务.通过AccessKeys分配Access Key ID和Access Key Se ...

  4. Android强制横屏+全屏的几种常用方法

    全屏: 在Activity的onCreate方法中的setContentView(myview)调用之前添加下面代码 1 requestWindowFeature(Window.FEATURE_NO_ ...

  5. Unity导航系统Navigation使用教程

    Unity开发VR之Vuforia 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- ...

  6. 安卓与Unity交互之-Android Studio创建Module库模块教程

    安卓开发工具创建Module库 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分 ...

  7. HDU - 5785:Interesting (回文树,求相邻双回文的乘积)

    Alice get a string S. She thinks palindrome string is interesting. Now she wanna know how many three ...

  8. NYOJ 85:有趣的数(打表,规律)

    85-有趣的数 内存限制:64MB 时间限制:3000ms 特判: No 通过数:8 提交数:12 难度:2 题目描述: 把分数按下面的办法排成一个数表. 1/1 1/2 1/3 1/4- 2/1 2 ...

  9. scrapy框架学习之路

    一.基础学习 - scrapy框架 介绍:大而全的爬虫组件. 安装: - Win: 下载:http://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted pip3 ...

  10. python编译hello

    pycharm无法找到解释器,将无法编译. 所以在编译之前进行统一设置 点击File,选择settings,点击 添加解释器 最后点击Apply.等待系统配置. 如果我们需要添加新的模块,点击绿色+号 ...