I have a fairly large VB Script project in which the primary script "includes" a number of "libraries" using the standard trick of reading file contents and running ExecuteGlobal
on them. Some of the libraries are pretty vast and written by various third parties.
I want to use Option Explicit
. If I make it the first line executed, however, some of those libraries blow up. But, if I move the directive to below my list of includes, I encounter the error Expected Statement
on that line. Even more confusing, if Option Explicit
appears at the top of one of the libraries (in the middle of the list of them), all is well. But, I wanted to remove (or comment that out) from any of the libraries and only enforce the restriction in my implementation script.
What's the rule about where Option Explicit
must appear? Does it have to be the first line or not? Why is it kosher for it to not be the first line when I apply it via an "include"? How can I achieve my objective?
Code Examples:
Option Explicit ' CAUSES RUNTIME ERROR IN A LIBRARY
Sub Include( sRelativeFilePath )
Dim oFs : Set oFs = CreateObject("Scripting.FileSystemObject")
Dim sThisFolder : sThisFolder = oFs.GetParentFolderName( WScript.ScriptFullName )
Dim sAbsFilePath : sAbsFilePath = oFs.BuildPath( sThisFolder, sRelativeFilePath )
ExecuteGlobal oFs.openTextFile( sAbsFilePath ).readAll()
End Sub
Include ".\SomeLib.vbs"
Include ".\SomeOther.vbs"
Include ".\YetAnother.vbs"
Vs
Sub Include( sRelativeFilePath )
Dim oFs : Set oFs = CreateObject("Scripting.FileSystemObject")
Dim sThisFolder : sThisFolder = oFs.GetParentFolderName( WScript.ScriptFullName )
Dim sAbsFilePath : sAbsFilePath = oFs.BuildPath( sThisFolder, sRelativeFilePath )
ExecuteGlobal oFs.openTextFile( sAbsFilePath ).readAll()
End Sub
Include ".\SomeLib.vbs"
Include ".\SomeOther.vbs"
Include ".\YetAnother.vbs"
Option Explicit ' CAUSES COMPILATION ERROR