How do I open a file for use globally in VBScript?

I am using the following to open a file for reading in a vbscript.  The script fails when I don't open the file in both the main section and the functions using it.  Is there a way to open the file once and make it available throughout?

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(myFile.txt", ForReading)
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Just do these commands outside of any functions or sunroutines. Also I like to prepend a 'g' or 's' to all global variables.

For example:

Const ForReading = 1
Set sobjFSO = CreateObject("Scripting.FileSystemObject")
Set sobjFile = sobjFSO.OpenTextFile(myFile.txt", ForReading)

Function Func1
' You can reference gobjFile here
End Function

Sub Sub1
' You can reference gobjFile here
End Function

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Opps...that last line should be 'End Sub'. But hopefuly you get the concept. Any variables / statements outside a function or subroutines are global or script variables and can be referenced by all functions in the same file.
Bill PrewIT / Software Engineering ConsultantCommented:
In addition to the above approach, I'll mention that it's actually enough just to DIM the variable you want to share outside of any functions, in the "main" body of the script.  Once the variable has been DIM'ed in that way, it can be referenced in any location in the code, and you could even have a subroutine or function that did the actual open of the file as long as the object you assign that too was DIM'ed outside of the routine.  Here's a simple example to demonstrate this:

Option Explicit
Dim PublicVar1
Wscript.Echo PublicVar1

Sub DoSub()
   PublicVar1 = "hello"
End Sub

Open in new window

jrslimAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.