Thursday, July 07, 2011

HTML Applications, VBScript, and Includes

Yes, you can use includes in HTA and VBScript applications, and there are a couple of ways to do this, but you need to control the environment quite well for success.

First off someone very helpful (ebgreen) posted a function you can use to add an include here, and while I mean him or her no disrespect, the delusion of this being called a class is bothersome. It's an include, plain and simple, not a class. Of course, his routine is hardly plain and simple. What I have gleaned from various sources on the Internet, and created is this:


' VBScriptWithIncFileFunctionBase.vbs
Option Explicit
Dim fso, oFile, sText
set fso = CreateObject("Scripting.FileSystemObject")
IncFile ""
 
sub IncFile(strFileName)
set oFile = fso.OpenTextFile(strFileName,1)
sText = oFile.ReadAll
oFile.close
ExecuteGlobal sText
end sub


Simple, but EBGreen's routine has error checking/handling. Our projects at work have employed EBGreen's function and it works well.

No matter which method you choose to employ you must pay close attention to location. If you deploy this to  a system, you'll need to be specific about where that IncFile is located and either specify it explicitly, or change to that folder to have it available when you begin this process.

Set WshShell = WScript.CreateObject("WScript.Shell") 
'Change to the Install Folder...
strSourceDir = "C:\ITSupport\Scripts\MyScript"
WshShell.CurrentDirectory = strSourceDir
Prefixing your first call to IncFile with a change of directory should set you up nicely to proceed with success. Alternatively, you can be explicit about the location of the file name specified:

IncFile strSourceDir & "\" 



There is no individual ownership when you are part of a team, it's the sum of the parts that makes you the RESILIENT team you need to be.