3

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?

3 Answers3

4

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.

Community
  • 1
  • 1
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
1

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)
WorkSmarter
  • 3,738
  • 3
  • 29
  • 34
  • Awesome You saved my day. But this one has one limitation in my project. My project is likely using both HTA and HTML files where HTML files will opened in IE by HTA automatically. since you are using `Set fso = CreateObject("Scripting.FileSystemObject")` this IE is prompting user to use ActiveX component, which i dont want.. any suggestions ? – Prabhu Vignesh Rajagopal Feb 20 '15 at 06:42
0

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

roblem
  • 527
  • 5
  • 8