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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Include code from other VBS files?

Status
Not open for further replies.

jasonp45

Programmer
Aug 23, 2001
212
US
I am running some VBScripts locally on an XP machine, not as part of a web project.

In an effort to modularize & re-use some of my common functions/operations I'd like to call external VBS files. Something like:

Option Explicit
IncludeFile("C:\cDate_Operations.vbs")
Set oDate_Ops = New cDate_Operations
MsgBox oDate_Ops.CurrentDate_YYYY_Mmm_DD

Private Sub IncludeFile (ByVal FileName)
Const ForReading = 1
Set oFSO = CreateObject("Scripting.FileSystemObject")
Dim f: set f = oFSO.OpenTextFile(FileName, ForReading)
Dim s: s = f.ReadAll()
'***This line doesn't work!:
Set cDate_Operations = Eval(s)
End Sub


Anyone know how to do this? Thanks!
 
Rather than Eval have you tried ExecuteGlobal ?

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Yes that was actually the first thing I tried, after finding a description of that here.

But that didn't work either. I think I'm implementing it incorrectly somehow.
 
Never mind - I figured it out. I screwed up the class I was calling in the other file!

This does indeed work:

Code:
IncludeFile("C:\cDate_Operations.vbs")
Set oDate_Ops = New Date_Operations
MsgBox oDate_Ops.CurrentDate_YYYY_Mmm_DD

Private Sub IncludeFile (ByVal FileName)
   Const ForReading = 1
   Set oFSO = CreateObject("Scripting.FileSystemObject")   
   Dim f: set f = oFSO.OpenTextFile(FileName, ForReading)
   Dim s: s = f.ReadAll()
   MsgBox s
   ExecuteGlobal s
End Sub
 
Look at using .WSF files. Naked .VBS files went out of style back around 1999. ;-)

faq329-3419
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top