Question

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??

Was it helpful?

Solution

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.

OTHER TIPS

FYI for those with the same problem in the future, to fix this:

  1. Open the .vbs in notepad
  2. Go to file and "save as"
  3. Right under the file name box, you will see a drop down menu for encoding. Choose ANSI.

Change in your code from:

Set objFile = objFSO.OpenTextFile(strFile, 1)
ExecuteGlobal objFile.ReadAll

to

Set objFile = objFSO.OpenTextFile(strFile, 1, False, -2)
ExecuteGlobal objFile.ReadAll

The last parameter in OpenTextFile function is TristateUseDefault = -2 (Open the file using the system default.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top