1
votes

I want to include a VBscript into another VBscript (kinda simulating OO in VBS), found something online and it seems Ok to me. I keep getting an "expected statement"-error on the ExecuteGlobal line:

Dim scriptLocation

Sub Main
    scriptLocation ="script2.vbs"
    Include(scriptLocation)
End Sub

Sub Include (strFile)
    Dim fsObj : Set fsObj = CreateObject("Scripting.FileSystemObject")
    Dim vbsFile : Set vbsFile = fsObj.OpenTextFile(strFile, 1, False)
    Dim myFunctionsStr : myFunctionsStr = vbsFile.ReadAll
    vbsFile.Close
    Set vbsFile = Nothing
    Set fsObj = Nothing
    ExecuteGlobal myFunctionsStr
End Sub

Anyone any idea?

3
What is the content of myFunctionsStr before you call ExecuteGlobal? Have you tried with something like a HelloWorld() function to check that the code is working in principle? Also the above is most definitely not your entire code, as it wouldn't do anything at all. - Ansgar Wiechers
the content should be: myFunctionsStr = vbsFile.ReadAll , and it should load another vbs file with a Main sub. - Bulki
I can see what it's supposed to do. I was asking what the actual value of strFunctionsStr is after the file was read. Add a line WScript.Echo strFunctionsStr before the ExecuteGlobal instruction. - Ansgar Wiechers
I recommend Windows Script Components as method of doing what you want to do - user69820

3 Answers

4
votes

An error message on an ExecuteGlobal line can mean:

  1. You succeeded in messing the ExecuteGlobal statement up. Surest way: add param list () as you did for the Include Sub call
  2. The code you loaded into myFunctionStr is to blame. If you do chaining/multiple includes this 'feature' of VBScript's error handling makes finding the culprit difficult. Easy way out: run the 'library' files with cscript.

BTW: There is no necessary relation between OOP and including code (libraries/modules).

1
votes

There nothing wrong with the builtin sub ExecuteGlobal. Try this:

ExecuteGlobal "*"

That will bring up expected statement error. But the problem, as you can see, lie inside the string. So, just debug your script2.vbs.

0
votes

Try to open script2.vbs in HexEditor. If your script encoded in UTF-8, then file contains 0x{ef bb bf} at the start. Method .ReadAll reads just all from file to myFunctionStr, including that 3 bytes. Remove that bytes from string or convert your script2.vbs to ANSI.