Have you ever wanted to include another .vbs file with all your common functions in a script?

Many people I have met thought it wasn’t possible.

But it is actually pretty easy to do with “ExecuteGlobal”

to make it even easier, I have created a small function which you can put in your scripts and use a simple

Include(“C:\functions.vbs”)

I have made 2 small example scripts to show you how to use it:

Functions.vbs:

Sub WriteLog(strMessage)
	Const FOR_APPENDING = 8

	strFileName = "C:\test.txt"

	Set objFS = CreateObject("Scripting.FileSystemObject")

	If objFS.FileExists(strFileName) Then
		Set oFile = objFS.OpenTextFile(strFileName, FOR_APPENDING)
	Else
		Set oFile = objFS.CreateTextFile(strFileName)
	End If

	oFile.WriteLine strMessage
End Sub

This file could be a shared file with all your common functions such as Log writer.

Include-Example.Vbs

Include("C:\functions.vbs")
WriteLog "Start of Script!"

Sub Include (strFile)
	'Create objects for opening text file
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objTextFile = objFSO.OpenTextFile(strFile, 1)

	'Execute content of file.
	ExecuteGlobal objTextFile.ReadAll

	'CLose file
	objTextFile.Close

	'Clean up
	Set objFSO = Nothing
	Set objTextFile = Nothing
End Sub

Then everytime you create a script you include the function and make sure to include the other vbs files in the beggning of the script.

Now you have access to all the functions from the other scripts, from inside your new script!

and the next time the management tells you to change the Log Format to a different date/time format, so simular. You just have to change the function in the shared file, instead of editing all your scripts!

But beware not to corrupt the shared file, cause it will make all your scripts fail 😉

Merry x-mas!