Thursday, 1 August 2013

VBScript - Call a function in another file


Well.. long time no blog. I've been learning a bit of VBScript, a bit more VBScript in truth. As usual I've played with it previously as part of QTP testing but never liked it. (you'll have to guess which I never liked...). One thing I tried to do recently was convert my 'game' files from Ruby to VBScript. I did it with JavaScript a while ago and my blood laptop died. The re-practice will do me good.



YouTube Channel: [WATCH, RATE, SUBSCRIBE]


A thing I have in the files is a class and function library. In Ruby it's easy to simply include a file with other stuff in, literally include somestuff.rb, then use whatever got included, gotta love it. Not so easy in VBScript. For brevity I'll jump straight to the solution.

NOTE: this is not MY solution, I trawled many a forum and grabbed the code, but in doing so didn't note the forum. SO APOLOGIES IN ADVANCE. Oh, and it was finished off by Majiec at BJSS, so ta for that!

Create a file called moduleCall.vbs and include the following:


Sub Include(file)

  Dim fso, f

  Set fso = CreateObject("Scripting.FileSystemObject")
  Set f = fso.OpenTextFile(file & ".vbs", 1)

  str = f.ReadAll

  f.Close

  ExecuteGlobal str

End Sub

' Now call Include and then call Doit
Include "The_Module"

Doit


Then create a file called The_Module.vbs and add the following:

Sub Doit
  MsgBox "Hello"
End Sub


Now run moduleCall.vbs and watch the pop-up open.

Gettit?

Pick up a more complete set of files at www.cyreath.co.uk/code/vbscript - the "005" set.

Mark.


Liked this post?



Read More









2 comments: