我使用Rob的脚本来打开文件对话框(这里的顶部文章),它显然应该在HTA中工作,但是我收到一个错误,上面说:
"ActiveX组件无法创建对象:'UserAccounts.CommonDialog'“
发布于 2015-08-02 10:51:10
这个功能可能对你有帮助!
BrowseForFile.vbs
'**************************************************************************************
' GetFileDlg() And GetFileDlgBar() by omen999 - may 2014 - http://omen999.developpez.com
' Universal Browse for files function
' compatibility : all versions windows and IE - supports start folder, filters and title
' note : the global size of the parameters cannot exceed 191 chars for GetFileDlg and 227 chars for GetFileDlgBar
'**************************************************************************************
Function GetFileDlg(sIniDir,sFilter,sTitle)
GetFileDlg=CreateObject("WScript.Shell").Exec("mshta.exe ""about:<object id=d classid=clsid:3050f4e1-98b5-11cf-bb82-00aa00bdce0b></object><script>moveTo(0,-9999);function window.onload(){var p=/[^\0]*/;new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(p.exec(d.object.openfiledlg('" & sIniDir & "',null,'" & sFilter & "','" & sTitle & "')));close();}</script><hta:application showintaskbar=no />""").StdOut.ReadAll
End Function
Function GetFileDlgBar(sIniDir,sFilter,sTitle)
GetFileDlgBar=CreateObject("WScript.Shell").Exec("mshta.exe ""about:<object id=d classid=clsid:3050f4e1-98b5-11cf-bb82-00aa00bdce0b></object><script>moveTo(0,-9999);function window.onload(){var p=/[^\0]*/;new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(p.exec(d.object.openfiledlg('" & sIniDir & "',null,'" & sFilter & "','" & sTitle & "')));close();}</script>""").StdOut.ReadAll
End Function
' sample test
sIniDir = "C:\Windows\Fonts\*"
sFilter = "All files (*.*)|*.*|Microsoft Word (*.doc;*.docx)|*.doc;*.docx|Adobe pdf (*.pdf)|*.pdf|"
sTitle = "GetFileDlg by omen999 2014 - omen999.developpez.com"
' (sIniDir + sFilter + sTitle) size doesn't exceed 191 chars (227 for GetFileDlgBar)
' MsgBox Len(Replace(sIniDir,"\","\\")) + Len(sFilter) + Len(sTitle)
' sIniDir must be conformed to the javascript syntax
rep = GetFileDlg(Replace(sIniDir,"\","\\"),sFilter,sTitle)
MsgBox rep & vbcrlf & Len(rep)
发布于 2015-08-02 14:06:37
正如注释中提到的@JosefZ,UserAccounts.CommonDialog
库仅在Windows中可用。但是,还有其他方法来显示"Open File"
对话框。
Shell.Application
对象具有一个BrowserForFolder()
函数,默认情况下,该函数显示一个对话框,要求您选择一个文件夹。但是,您可以通过使用值的组合,以多种方式配置此对话框。例如,如果包含BIF_BROWSEINCLUDEFILES
标志,则对话框还将显示文件夹之外的文件。
下面是一个很小的示例,演示如何让BrowserForFolder
对话框显示文件并提示用户选择文件:
' BROWSEINFO Flags...
Const BIF_NONEWFOLDERBUTTON = &H0200 ' Hide the [New Folder] button
Const BIF_BROWSEINCLUDEFILES = &H4000 ' Show files in addition to folders
' ShellSpecialFolderConstants...
Const ssfDESKTOP = 0
Dim objStartIn, objFile
With CreateObject("Shell.Application")
' Specify the folder the dialog should start in...
Set objStartIn = .NameSpace(ssfDESKTOP) ' Start in a special folder
Set objStartIn = .NameSpace("c:\") ' Or, start in custom path
' Args = (parent window, dialog title, flags, start folder)
Set objFile = .BrowseForFolder(0, "Select a file:", _
BIF_BROWSEINCLUDEFILES Or BIF_NONEWFOLDERBUTTON, objStartIn)
End With
If Not objFile Is Nothing Then
WScript.Echo objFile.Self.Path
End If
当然,用户仍然可以选择一个文件夹。没有办法阻止这一切的发生。但是您可以检查返回的项,看看它是否是一个文件夹,并提示它们重新选择(可能在循环中)。
If objFile.Self.IsFolder Then
' Invalid
End If
https://stackoverflow.com/questions/31770606
复制相似问题