Prabhu Vignesh Rajagopal
Prabhu Vignesh Rajagopal

Reputation: 1068

How to get variables from another vbscript (another .vbs file)

I have a file which has following variables.

Dim apple(10)
apple(0)= "banana"
apple(1)= "2 banana"
apple(2)= "3 banana"

and these variables are in script/test/test.vbs

Now i have another file which has following

MSGBOX apple(0)
MSGBOX apple(1)

which is in script/main.vbs

how to make these possible?

Upvotes: 3

Views: 5111

Answers (3)

roblem
roblem

Reputation: 591

If this is a webpage you can use an html include to make it work.. (usually stick it in the location you would want the content. (head tag or body tag etc...)

<!--#include file="../include/FileWithVaraibles.htm"-->

using #include

Upvotes: 0

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

For .HTA/.HTML/.WSF don't try to re-invent the wheel but use the src attribute of the script tag.

For plain .VBS use ExecuteGlobal as demonstrated here:

Dim gsLibDir : gsLibDir = "M:\lib\kurs0705\"
Dim goFS     : Set goFS = CreateObject( "Scripting.FileSystemObject" )
ExecuteGlobal goFS.OpenTextFile( gsLibDir & "BaseLib.vbs" ).ReadAll()

In either case put the code to be re-used into the included file; don't waste work on fancy Include Subs/Functions that provide nothing more than a one-liner.

Upvotes: 4

WorkSmarter
WorkSmarter

Reputation: 3808

Yes it is possible. A little additional work needs to go into including the external file. I based the work below on this article.

Contents of test.vbs file

Sub CustomInclude(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

'Set variables here
Dim apple(10)
apple(0)= "banana"
apple(1)= "2 banana"
apple(2)= "3 banana"

' Now call subroutine to include my external file.
CustomInclude "../main"  'Use of relative folder structure

Contents of main.vbs

MSGBOX apple(0)
MSGBOX apple(1)
MSGBOX apple(2)

Upvotes: 1

Related Questions