VBS Sub/Functions

remzaki

n00b
Joined
Nov 4, 2012
Messages
16
Hi All,

Got a problem here...

I have this TestA.vbs file that calls TestB.vbs file:
test 'Calling the test Sub
Sub test
Set oShell = CreateObject("Scripting.FileSystemObject")
Set ReadFile = oShell.OpenTextFile("C:\TestB.vbs")
myScript = ReadFile.ReadAll
Execute(myScript)
ReadFile.Close
...lines of code...
End Sub​

And inside the TestB.vbs file I have 2 subs:
Sub subA
...lines of code...
subB 'calling s Sub named subB
...lines of code...
End Sub

Sub subB
WScript.Echo "hi"
End Sub​

When ever I run the TestA.vbs I get an error:
Line: 0
Column: 1
Error: Type mismatch: 'subB'
Code: 800A000D
Source: Microsoft VBScript runtime error
System: The data is invalid.​


Been looking all around the internet for answers, but failed. Help :confused:
 
Last edited:
I copied and pasted your code (except for "...lines of code...") into 2 separate files named exactly like you have and I get a message box with "hi" as expected when running TestA.vbs. Maybe it's something in the "...lines of code..." that is causing the error.
 
For reference, here's what I have:

TestA.vbs
Code:
Set oShell = CreateObject("Scripting.FileSystemObject")
Set ReadFile = oShell.OpenTextFile("TestB.vbs")
myScript = ReadFile.ReadAll
Execute(myScript)
ReadFile.Close

TestB.vbs
Code:
Sub subA
	subB 'calling s Sub named subB
End Sub

Sub subB
	WScript.Echo "hi"
End Sub

subA 'I added this since you do nott have a call to subA in the OP.  Maybe it's in the ...lines of code...
 
I found the culprit.

The script calling the file is inside a Sub function. I updated the OP with this scenario.

Could you please try it on your end?
I wanna know what is happening...
 
I get the same error as you now.

I think the error may be related to how you're running TestB. If I copy and paste the code from TestB directly into Sub test (inside TestA.vbs) then it runs fine.

Is there any particular reason you're calling the second script the way you are and not doing something like this?:

TestA.vbs
Code:
test 'Calling the test Sub
Sub test
	Set oShell = WScript.CreateObject("WScript.Shell")
	oShell.Run "TestB.vbs"
	Set oShell = Nothing
	'...lines of code...
End Sub
 
Actually there is a particular reason. But anyway seems like the Sub being called inside the sub while they were ran inside a sub, is gone on the memory. So...I just had to put it in other function so that it can call it without errors.

Post might as well be closed. Thank you for your replies sfam198 :)
 
Back
Top