Skip to content

Instantly share code, notes, and snippets.

@codeartery
Last active April 25, 2024 06:08
Show Gist options
  • Save codeartery/0ca6057bcc52e34f8b50b6772ecc8cf1 to your computer and use it in GitHub Desktop.
Save codeartery/0ca6057bcc52e34f8b50b6772ecc8cf1 to your computer and use it in GitHub Desktop.
Import/include/using code from an external VBScript file.
Function Import( vbsFile )
REM@description
' Import/include/using code from an external VBScript file.
REM@author
' Jeremy England, http://codeartery.com/
REM@params
' vbsFile <string> - A relative, absolute, or URL path to a file containing vbscript code.
REM@returns
' Import <bool> - Returns False if the import failed, and True if it succeeded.
REM@mini
' Function Import(p):Import = False:Dim f,m:Set f=CreateObject("Scripting.FileSystemObject"):If(f.FileExists(f.GetAbsolutePathName(p)))Then:ExecuteGlobal(f.OpenTextFile(f.GetAbsolutePathName(p)).ReadAll):Import=-1:Exit Function:End If:Set m=CreateObject("Microsoft.XMLHTTP"):On Error Resume Next:m.Open "GET",p,0:If Not(Err.Number=-2147012890)Then:m.Send:If(m.Status=200)Then:ExecuteGlobal(m.ResponseText):Import=-1:End If:End If:End Function
Import = False
Dim oFso, oMxh
Set oFso = CreateObject( "Scripting.FileSystemObject" )
If( oFso.FileExists( oFso.GetAbsolutePathName( vbsFile ) ) )Then
ExecuteGlobal( oFso.OpenTextFile( oFso.GetAbsolutePathName( vbsFile ) ).ReadAll() )
Import = True
Exit Function
End If
Set oMxh = CreateObject( "Microsoft.XMLHTTP" )
On Error Resume Next
Call oMxh.Open( "GET", vbsFile, False )
If Not( Err.Number = -2147012890 )Then
Call oMxh.Send()
If( oMxh.Status = 200 )Then
ExecuteGlobal( oMxh.ResponseText )
Import = True
End If
End If
End Function
REM@usage
' Put the full or mini class/sub/function in your script to use.
Function Import(p):Import = False:Dim f,m:Set f=CreateObject("Scripting.FileSystemObject"):If(f.FileExists(f.GetAbsolutePathName(p)))Then:ExecuteGlobal(f.OpenTextFile(f.GetAbsolutePathName(p)).ReadAll):Import=-1:Exit Function:End If:Set m=CreateObject("Microsoft.XMLHTTP"):On Error Resume Next:m.Open "GET",p,0:If Not(Err.Number=-2147012890)Then:m.Send:If(m.Status=200)Then:ExecuteGlobal(m.ResponseText):Import=-1:End If:End If:End Function
' Online files work
Import "https://www.example.com/location/of/file.vbs"
' Offline files work
Import "c:\location\of\file.vbs"
' Relative paths work
Import "../file.vbs"
' Returns whether it succeeded with the import
Dim wasImported
wasImported = Import( "c:\location\of\file.vbs" )
If Not( wasImported )Then
MsgBox "Failed to Import!", vbCritical
WScript.Quit()
End If
' The extension doesn't have to be *.vbs, the file just has to contain valid VBScript code.
Import "c:\location\of\file.txt"
@twofingerrightclick
Copy link

This is so useful!

@RoyceSG
Copy link

RoyceSG commented Jan 17, 2023

Error = ".vbs(3, 139) Microsoft VBScript compilation error: Redefined name"
This error occurs when trying to import a Function with parameters.
Thank you very much anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment