[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[技术讨论] vbs打开保存文件对话框的方式

本帖最后由 jyswjjgdwtdtj 于 2024-4-6 19:23 编辑
  1. '1 shell.application 功能少 界面丑
  2. hwnd=0 '父窗口句柄
  3. title="select files" '标题
  4. ioption=0 '详见https://learn.microsoft.com/zh-cn/windows/win32/api/shlobj_core/ns-shlobj_core-browseinfoa中ulflags
  5. '其中&h4000可以使选择文件与文件夹 但文档表示该函数会返回folder对象 选择文件时可能会出现奇怪的错误
  6. RootFolder="" '起始目录 详见https://learn.microsoft.com/zh-cn/windows/win32/api/shldisp/ne-shldisp-shellspecialfolderconstants
  7. s=selectfilename(hwnd,title,ioption,RootFolder)
  8. function selectfilename(hwnd,title,ioption,RootFolder)
  9. set s=CreateObject("shell.application")
  10. set f=s.BrowseForFolder(hwnd,title,ioption,rootfolder)
  11. selectfilename f.self.path
  12. end function
  13. '2 excel.application 个人认为最靠谱 最优雅 无需vba
  14. fileFilter = "Vbscript Files (*.vbs|*.vbe),*.vbs;*.vbe,All Files (*.*),(*.*)"
  15. title="select files"
  16. multiselect=true' true即可选择多个文件 false单选
  17. s=selectfilename(fileFilter,title,multiselect)
  18. function selectfilename(fileFilter,title,multiselect)
  19. Set e=CreateObject("Excel.Application")
  20. e.visible=false
  21. filterindex=0'以上默认筛选器的编号
  22. buttontext="" '仅在mac电脑上支持???
  23. selectfilename=e.GetOpenFilename (fileFilter, FilterIndex, Title, ButtonText, MultiSelect)
  24. e.quit
  25. end function
  26. 'GetSaveAsFilename 保存文件 用法相似
  27. '3 excel.application 可惜我的04版wps不支持  没法测试
  28. '字面意思的常量
  29. filefilter="Images", "*.gif; *.jpg; *.jpeg"
  30. initfolder="C:\"
  31. selectfilename(fileFilter,initfolder)
  32. function selectfilename(fileFilter,initfolder)
  33. Const msoFileDialogOpen = 1
  34. Const msoFileDialogSaveAs = 2
  35. Const msoFileDialogFilePicker = 3
  36. Const msoFileDialogFolderPicker  = 4
  37. Set oExcel = CreateObject("Excel.Application")
  38. Set FileDialog = oExcel.FileDialog(msoFileDialogFilePicker)
  39. FileDialog.Filters.Add fileFilter, 1
  40. FileDialog.InitialFileName = initfolder
  41. FileDialog.show()
  42. selectfilename = FileDialog.SelectedItems(1)
  43. end function
  44. '4 html中的<input type=file/> 功能受限 很不优雅(要打开别的程序)
  45. 'mshta.exe
  46. s=selectfilename()
  47. function selectfilename()
  48. Set w= CreateObject("WScript.Shell")
  49. Set e = w.Exec("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>""")
  50. selectfilename= o.StdOut.ReadLine
  51. end function
  52. 'internetexplorer.application 有个ie图标很讨厌 效率很低
  53. s=selectfilename()
  54. function selectfilename()
  55. Set IE = CreateObject("InternetExplorer.Application")
  56. With IE
  57. .Visible = False
  58. .Navigate("about:blank")
  59. With .Document.createelement("input")
  60. .type="file"
  61. .click()
  62. selectfilename=.value
  63. End With
  64. .Quit
  65. End With
  66. end function
  67. 'htmlfile 不可行
  68. Set h = CreateObject("htmlfile")
  69. With h.createelement("input")
  70. .type="file"
  71. .click()'没反应
  72. selectfilename=.value
  73. End With
  74. '5 MSComDlg.CommonDialog/UserAccounts.CommonDialog 可惜已经不支持了
  75. '6 HtmlDlgHelper.HtmlDlgHelper 功能最好 但不优雅
  76. StrIniDir = ""
  77. StrFilter = "Msi Files (*.msi)|*.msi|All files (*.*)|*.*|Microsoft Word (*.doc;*.docx)|*.doc;*.docx|Adobe pdf (*.pdf)|*.pdf|"
  78. StrTitle = "Select MSI file"
  79. s=GetFileDlg(Replace(StrIniDir,"\","\\"),StrFilter,StrTitle)
  80. Function selectfilename(sIniDir,sFilter,sTitle)
  81. selectfilename=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
  82. End Function
  83. '这个组件不支持直接在vbs中运行 它还可以打开保存文件对话框 选择颜色对话框 另存为对话框
复制代码

回复 1# jyswjjgdwtdtj
fileFilter = "Vbs Files (*.vbs),*.vbs,All Files (*.*),(*.*)" ' 似乎并不支持|来同时识别两种后缀名

其实可以同时识别两种文件拓展名的:
  1. fileFilter = "Visual Basic Files (*.bas; *.txt), *.bas;*.txt"
复制代码
请参阅:https://learn.microsoft.com/zh-cn/office/vba/api/excel.application.getopenfilename
Why join the navy, if you can be a pirate? - Steve Jobs

TOP

返回列表