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?

0

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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)

1 Comment

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 ?
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.