Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I link one vbscript file to another? 2

Status
Not open for further replies.

riccy

Programmer
Oct 30, 2001
4
DE
:-I I have one vbscript-file (*.vbs, not for webpage-building) that is very long. Can I divide it into two or more separate files and link them together? I imagine, that in file one is the main-sub that calls subs or functions which are declared/ defined in another file.

Thanx, riccy |-0
 
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=&quot;mainscript&quot;>
    <script language=&quot;vbscript&quot; src=&quot;opendialog.vbs&quot; />

    <script language=&quot;vbscript&quot;>
        Dim strChosenFile

        strChosenFile = OpenDialog(&quot;Pick a file!&quot;)
        MsgBox &quot;You picked:&quot; & 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(&quot;MSComDlg.CommonDialog&quot;)
    With dlgFileOpen
        .CancelError = True
        .DialogTitle = strTitle
        .Filter = _
            &quot;Text (*.txt)|*.txt|Logs (*.log)|*.log|&quot; & _
            &quot;Html (*.htm;*.html;*.hta;*.asp)|&quot; & _
            &quot;*.htm;*.html;*.hta;*.asp&quot;
        .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 &quot;job&quot; and the job has two &quot;scripts&quot; (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!
 
:-D Thanx a lot for fast answering. Can you tell me, please, how I can find out, which version is installed on my computer?

riccy
 
By the way...

In the included example file above, just before End Function there should have been a statement:
Code:
Set dlgFileOpen = Nothing
Sorry about the goof!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top