Why does VBScript say "Invalid Character" when using ExecuteGlobal to import a .vbs function library file?

Eptin picture Eptin · Dec 21, 2012 · Viewed 18.4k times · Source

I am encountering a "Invalid Character" error in my VBscript! In particular, within this block of code:

'*******************************************************************
'Import Code
'by Cheyne Wallace
'November 2008

'When using only VBscript (not QTP), this code will import any function library passed into it.
'Copy this function into a file, then use it to bring in various other function libraries.
'Usage:
'   Import "Library.vbs"

Sub Import(strFile)
    Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
    Dim wss : Set wss = CreateObject("WScript.Shell")
    strFile = wss.ExpandEnvironmentStrings(strFile)
    strFile = objFSO.GetAbsolutePathName(strFile)
    Set objFile = objFSO.OpenTextFile(strFile, 1)
    ExecuteGlobal objFile.ReadAll
    objFile.Close : Set objFSO = nothing
    Set wss = Nothing
End Sub

The error states "Microsoft VBScript compilation error. Invalid Character. Code: 800A0408" on Char: 2, Line 206 which is the very first letter on ExecuteGlobal objFile.ReadAll (character 1 is a tab).

I have typed and retyped the line, as well as the surrounding line breaks. Still, it keeps saying 'Invalid character'. What is going on??

Answer

Eptin picture Eptin · Dec 21, 2012

As it turns out, the problem was not with ExecuteGlobal, but instead with the .vbs file I was attempting to import. The file I was importing was not in ANSI encoding. If VBScript has a problem with the file you are importing, it will report the error at the character and line for the beginning of ExecuteGlobal (which admittedly causes confusion).

Open the file you are attempting to import, convert it to ANSI and everything should work.