Guest User

Untitled

a guest
Jun 7th, 2026
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | Source Code | 0 0
  1. ' VBScript FileOpen dialogue.
  2. ' no special requirements. Tested up to Win11 22H2.
  3.  
  4. Dim Dlg, s
  5. Set Dlg = new Dialogs
  6.  
  7. '-- To provide for 1 or more file type filters just put the name followed by one or more file
  8. '-- extensions. The class with construct the filter as long as the paramter is formatted
  9. '-- like this comma-delimited sample: type, extension, type, extension, type, extension
  10. '-- ....etc. There can be any number of filters. Use "All Files, *" to also have a general filter
  11. '-- option that shows all files. If more than 1 extension applies to a file type, separate each
  12. '-- with a space, as done here with the "HTML Files" type.
  13.  
  14. s = Dlg.FileOpenDlg("Choose File", "", "Text Files, txt, All Files, *")
  15.  
  16. MsgBox s
  17.  
  18. Class Dialogs
  19. Private Cls_Dlg, Cls_HTA, Cls_IE2
  20.  
  21. Public Function FileOpenDlg(Title, StartDir, sFilter) '-- openfiledlg([initFile as Variant][, initDir as Variant][, Filter as Variant][, Title as Variant])
  22. Dim Cls_Filter, Boo1, i2_c
  23. On Error Resume Next
  24. Boo1 = InitDlg()
  25. If Boo1 = False Then
  26. CleanUp
  27. Boo1 = InitDlg() '-- sometimes the first init fails. ?? Reason unknown.
  28. End If
  29. If Boo1 = False Then Err.Raise 1, "Dialogs Class", "Failed to initialize HtmlDlgHelper object": Exit Function
  30.  
  31. If Len(sFilter) > 0 Then
  32. Cls_Filter = GetFilterString(sFilter)
  33. FileOpenDlg = Cls_Dlg.openfiledlg(, CStr(StartDir), CStr(Cls_Filter), CStr(Title))
  34. Else
  35. FileOpenDlg = Cls_Dlg.openfiledlg(, CStr(StartDir), CStr("All Files(*.*)|*.*|"), CStr(Title))
  36. End If
  37.  
  38. If Len(FileOpenDlg) = 0 Then Exit Function
  39.  
  40. '--strange HTMLDlgHelper behavior. Returns a string ending with nulls.
  41. '-- The nulls won't affect using the string, but they will matter if you test the string.
  42. '-- For instance: If UCase(Right(s, 3)) = "TXT" Then .... That won't work unless the nulls are snipped.
  43. '-- so check for nulls. If first null is first character that will return "". Otherwise there's a path string to
  44. '-- extract from the string buffer.
  45. i2_c = InStr(FileOpenDlg, Chr(0))
  46. If i2_c > 1 Then FileOpenDlg = Left(FileOpenDlg, (i2_c - 1))
  47.  
  48. CleanUp
  49. End Function
  50.  
  51. Private Function GetFilterString(sFilter)
  52. Dim A1, i2, s1
  53. On Error Resume Next
  54. A1 = Split(sFilter, ",")
  55. i2 = 0
  56. Do While i2 < UBound(A1)
  57. GetFilterString = GetFilterString & Trim(A1(i2))
  58. s1 = Trim(A1(i2 + 1))
  59. s1 = "*." & s1
  60. s1 = Replace(s1, " ", ";*.")
  61. GetFilterString = GetFilterString & "(" & s1 & ")|" & s1 & "|"
  62. i2 = i2 + 2
  63. Loop
  64. End Function
  65.  
  66. Private Function InitDlg()
  67. Dim Cls_iCount, Cls_IE, Cls_SHAp
  68. '-- ugly hack required to get around problem of HTMLDlgHelper.HTMLDlgHelper being a control that needs to be sited.
  69. '-- it's also not marked as safe for scripting. This method dynamically creates an HTA, then gets the window.
  70. InitDlg = False
  71. CreateObject("WSCript.Shell").Run "MSHTA.EXE ""javascript:new ActiveXObject('InternetExplorer.Application').PutProperty('ID1', window);""", 0, False
  72.  
  73. '-- Next step: go through open windows and retrieve the window object for just-created HTA.
  74. Set Cls_SHAp = CreateObject("Shell.Application")
  75. On Error Resume Next
  76. Cls_iCount = 1
  77. Do Until Cls_iCount = 10
  78. For Each Cls_IE In Cls_SHAp.Windows
  79. If IsObject(Cls_IE.GetProperty("ID1")) Then
  80. Set Cls_IE2 = Cls_IE
  81. Exit Do
  82. End If
  83. Next
  84. WScript.sleep 100
  85. Cls_iCount = Cls_iCount + 1
  86. Loop
  87. Set Cls_SHAp = Nothing '-- done with Shell.Application.
  88. If Not IsObject(Cls_IE2) Then Exit Function
  89.  
  90. Set Cls_HTA = Cls_IE2.GetProperty("ID1") '-- Cls_HTA is now an HTMLWindow2 object.
  91. '-- instantiate the HTMLDlgHelper.HTMLDlgHelper control.
  92. Cls_HTA.document.body.innerHTML = "<OBJECT ID=Dlg CLASSID=CLSID:3050F4E1-98B5-11CF-BB82-00AA00BDCE0B> </object>"
  93. Set Cls_Dlg = Cls_HTA.Dlg.Object '-- get a reference to the control.
  94. Cls_IE2.quit '-- this seems to be necessary, but if done elsewhere it may cause an odd lag of 4-5 seconds.
  95.  
  96. If IsObject(Cls_Dlg) Then InitDlg = True
  97. End Function
  98.  
  99. Private Sub CleanUp()
  100. On Error Resume Next
  101. Set Cls_Dlg = Nothing
  102. If IsObject(Cls_HTA) Then
  103. Cls_HTA.Close
  104. Set Cls_HTA = Nothing
  105. End If
  106. If IsObject(Cls_IE2) Then
  107. Cls_IE2.quit
  108. Set Cls_IE2 = Nothing
  109. End If
  110. End Sub
  111.  
  112. '-- this shouldn't be necessary because each HTA is closed after a call is made.
  113. Private Sub Class_Terminate()
  114. CleanUp
  115. End Sub
  116.  
  117. End Class
Advertisement
Add Comment
Please, Sign In to add comment