This isn't too bad, actually.
The problem is you have to advance to the next level of WSH scripting: using a .wsf file instead of a .vbs file, at least for your main "program" (script).
First upgrade your scripting engines, host, and docs to something recent like version 5.6:
Software:
Docs:
Here is a "main" script I named mainscript.wsf:
Code:
<job id="mainscript">
<script language="vbscript" src="opendialog.vbs" />
<script language="vbscript">
Dim strChosenFile
strChosenFile = OpenDialog("Pick a file!")
MsgBox "You picked:" & vbCrLf &_
strChosenFile
</script>
</job>
Here is the script file containing the reusable function, called opendialog.vbs:
Code:
'Reusable file-open dialog function.
'This file is meant to be included into
'a Windows Script File (.wsf)
Function OpenDialog(strTitle)
'CommonDialog control constants.
Const cdlCancel = &H7FF3
Const cdlOFNFileMustExist = &H1000
Const cdlOFNHideReadOnly = &H4
Dim dlgFileOpen
'Initialize CommonDialog control.
Set dlgFileOpen = _
CreateObject("MSComDlg.CommonDialog")
With dlgFileOpen
.CancelError = True
.DialogTitle = strTitle
.Filter = _
"Text (*.txt)|*.txt|Logs (*.log)|*.log|" & _
"Html (*.htm;*.html;*.hta;*.asp)|" & _
"*.htm;*.html;*.hta;*.asp"
.FilterIndex = 1
.Flags = _
cdlOFNFileMustExist + _
cdlOFNHideReadOnly
.MaxFileSize = 512
End With
On Error Resume Next
dlgFileOpen.ShowOpen
If Err.number <> cdlCancel Then
OpenDialog = dlgFileOpen.FileName
End If
End Function
You'll note that the .wsf file looks little odd because of the XML tags. Don't worry, these are pretty simple to deal with and are documented pretty well.
This .wsf has one "job" and the job has two "scripts" (which don't even have to be in the same language!).
You start a .wsf just like a .vbs - just double-click the icon.
Just cut each of the two listings above out, paste them into their properly named files, and double-click on the one called
mainscript.wsf to execute it.
Hope this answers your question!