Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for comdlgfo.vbs

(view source code of comdlgfo.vbs as plain text)

  1. ' A script to demonstrate the File Open dialog available in COMDLG32.OCX
  2. ' Written by Rob van der Woude
  3. ' http://www.robvanderwoude.com
  4.  
  5. Option Explicit
  6.  
  7. Dim strSelected
  8.  
  9. strSelected = FileOpenDialog( "", "VBScript files|*.vbs" )
  10. If strSelected = "" Then
  11. 	If Err Then
  12. 		WScript.Echo "Error: " & Err.Description
  13. 	Else
  14. 		WScript.Echo "No file selected"
  15. 	End If
  16. Else
  17. 	WScript.Echo "Selected file: """ & strSelected & """"
  18. End If
  19.  
  20.  
  21. Function FileOpenDialog( myStartDir, myFileType )
  22. 	Dim objDialog, strFile, wshShell
  23.  
  24. 	Const OFN_HIDEREADONLY = &H4
  25. 	Const OFN_CREATEPROMPT = &H2000
  26. 	Const OFN_EXPLORER = &H80000
  27. 	Const OFN_LONGNAMES = &H200000
  28.  
  29. 	strFile = ""
  30.  
  31. 	On Error Resume Next
  32. 	Set objDialog = CreateObject( "MSComDlg.CommonDialog" )
  33. 	If Err Then
  34. 		MsgBox Err.Description & vbCrLf & vbCrLf & "This script requires COMDLG32.OCX." & vbCrLf & vbCrLf & "Please make sure it is installed and registered.", , "COMDLG32 not registered"
  35. 	Else
  36. 		If myStartDir = "" Then
  37. 			Set wshShell = WScript.CreateObject( "Wscript.Shell" )
  38. 			objDialog.InitDir = wshShell.CurrentDirectory
  39. 			Set wshShell = Nothing
  40. 		Else
  41. 			objDialog.InitDir = myStartDir
  42. 		End If
  43. 		If myFileType = "" And InStr( myFileType, "|*." ) > 5 Then
  44. 			objDialog.Filter     = myFileType
  45. 			objDialog.DefaultExt = Mid( myFileType, InStrRev( myFileType, "." ) + 1 )
  46. 		Else
  47. 			objDialog.Filter     = "All files|*.*"
  48. 		End If
  49. 		strFile = String( 260, Chr(0) )
  50. 		objDialog.MaxFileSize = 260
  51. 		objDialog.Flags = OFN_EXPLORER Or OFN_LONGNAMES Or OFN_CREATEPROMPT Or OFN_HIDEREADONLY
  52. 		objDialog.ShowOpen
  53. 		strFile = objDialog.FileName
  54. 	End If
  55.  
  56. 	On Error Goto 0
  57. 	Set objDialog = Nothing
  58.  
  59. 	FileOpenDialog = strFile
  60. End Function
  61.  

page last uploaded: 2017-04-06, 13:33