Is there a reason a call to Split() should work in one instance and not another? [closed] - vbscript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 days ago.
This post was edited and submitted for review 3 days ago.
Improve this question
I'm writing a script (vbscript) to generate Components that represent code modules (or whatever you'd call the abstraction of a C++ Class for C) in C. Each Component will have Elements for all the structs and enumerations in each module, as well as an interface for the defined functions in the module.
A separate Python script is extracting all of the pertinent information from the source and header files and storing them in a separate file, in a format that is easy for vbscript to deal with (a previous version was directly parsing the code in EA via vbscript, one file at a time, but the limitations of InStr were too great and the pull of Python's regular expressions package were also too great.
My script is working fine for enumerations and structs, and is working fine for functions in the interface--until I start adding Parameters to the Method elements.
One particular call to Split() is silently crashing the script. The call is verbatim to several other calls that are working perfectly fine, and the error box that I get when I execute that line in Debug mode is wordless, with only an OK button and a blue 'i' icon.
Here's a function that contains a call to Split() where the call is working correctly:
Function CreateStructs()
dim structsEnd
structsEnd = false
do while not structsEnd
line = file.ReadLine()
splitLine = Split(line, "#")
Select Case splitLine(0)
Case "STRUCT BEGIN"
CreateStruct splitLine(1)
Case "STRUCTURES END"
structsEnd = true
End Select
loop
End Function
And here's where it's not working:
Function CreateFunct(functionName, ByRef functionList, ByRef functPosition)
dim newFunction as EA.Method
dim newParams as EA.Collection
dim newParam as EA.Parameter
dim position, functEnd, access, returnType
dim newFunctionStereotype, newFunctionReturnType
functEnd = false
line = file.ReadLine()
newFunctionStereotype = Split(line, "#")(1)
line = file.ReadLine()
newFunctionReturnType = Split(line, "#")(1)
set newFunction = functionList.AddNew(functionName, newFunctionReturnType)
newFunction.Stereotype = newFunctionStereotype
newFunction.Pos = functPosition
do while not functEnd
line = file.ReadLine()
splitLine = Split(line, "#")
Select Case splitLine(0)
Case "ARGUMENTS BEGIN"
'CreateArguments newFunction
Case "FUNCT END"
functEnd = true
End Select
loop
newFunction.Update()
End Function
Both calls are within the do while blocks. The variables line and splitLine are "globally" defined in a separate script file that has been !INC'd
I'm at a loss for what to do and any help is greatly appreciated.
Edit: The variable being split is not empty when the silent crash happens, and the file being read from is not at EOF.

Related

Why doesn't this VBScript automation work? [duplicate]

I am getting the 800A0414 error in lines 7 and 12 of this script:
Module Module1
Dim p
Sub Main()
CreateObject("Wscript.Shell").Run("program.bat", 0, True)
p = Process.GetProcessesByName("program")
If p.Count > 0 Then
WScript.Sleep(300000)
Else
CreateObject("Wscript.Shell").Run("program clean up.bat", 0, True)
End If
End Sub
Private Function WScript() As Object
Throw New NotImplementedException
End Function
End Module
I am trying to run a batch script, that starts a process, then wait until the process terminates, then run another batch script. I also do not want any command boxes being shown. If their is a easier way please let me know.
Thanks for your help
When you enclose a procedure's argument list in parentheses, you must use the Call keyword:
Call CreateObject("WScript.Shell").Run("program.bat", 0, True)
If you omit the Call keyword, you must also drop parentheses:
CreateObject("WScript.Shell").Run "program.bat", 0, True
To complete what's been said before:
When Call keyword is used to call a procedure (i.e. sub or function) the arguments must be enclosed in parentheses, except when the procedure has no arguments in which case the parentheses are optional. For example all the statements:
Call test()
Call test
Call test(1,2)
are valid, but not this one:
Call test 1
When calling a procedure without using the Call keyword, the parentheses can only be used when either the procedure has zero or one argument or the procedure has a return value (i.e. is a function) and its value is used in the same statement. For example all the statements:
test()
test(1)
test(1,2)
a = test
a = test(1,2)
a = test(test(1,2),2)
are valid, except the third one which has more than one argument. In case it's not clear, the inner call of "test" in the last statement is valid because its return value is used as an argument to another call.
Note that whenever parentheses is used in this text, it is meant to imply the possible comma-separated values as well.
Seems to me this is a VB.NET, not VBScript code.
You have Shell function in VB.NET (and other methods).
Anyway, Run returns any error code returned by the program, and if you
store that result in a variable, you can use parentheses in this case.
Dim lResult As Long
lResult = CreateObject("Wscript.Shell").Run("program.bat", 0, True)
The rest was answered by #Helen.

What is the return type of my VBScript function?

This question is mostly out of interest to understand the functionality of VBScript better. I recognize that I can simply do some casting to know what to expect from my code, but in my situation I want to understand why casting, or any "workaround", is needed. For simplicity, here's the basic idea of my code:
variable1 = 1
Public Function findSomethingInATextString(par1, par2)
[...searching with a Do Until loop code here...]
Result = 1
If([par2 is found in par1]) Then
Result = 0
End If
Return Result
End Function
variable1 = findSomethingInATextString("Hello World", "Hello")
When I run this I get a Type Mismatch error. I don't understand why that's the case. variable1 is an integer and findSomethingInAString() returns an integer. They appear to be the same data type.
I'm working in a restricted environment where I can't do much debugging (it's painfully slow to code in this program...). So at the moment I'm unable to say what data type this is coming out as - I just know that it's apparently not integer.
After all that, and to make sure my question is clear, I'm intrigued to know what the return type of my function is (if someone happens to know), but my real question is: Why isn't the return type matching with variable1?
Use the minimal script
Return
Output
cscript 36633603.vbs
...36633603.vbs(1, 1) Microsoft VBScript runtime error: Type mismatch: 'return'
to prove to yourself, that just mentioning return in a VBScript will throw a type mismatch error.
Believe JosefZ's comment that VBScript returns function values by assigning to the function's name. Better: Read the docs (before you try to write code).
Evidence:
Function f1()
f1 = 1
End Function
WScript.Echo f1(), TypeName(f1())
Output:
cscript 36633603.vbs
1 Integer

For Each not initialized [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
My function is used to replace an apostrophe, i.e. ' in any of an object's properties. The function uses the TypeLib library to achieve this, by looping through all the object's members, i.e.
Public Function EscapeWildCards(ByRef obj As Object, _
Optional ByVal bEscape As Boolean = True) As Boolean
On Error GoTo EscapeWildCards_Err
Dim tTLI As TLIApplication
Dim tMem As MemberInfo
Dim tInvoke As InvokeKinds
Dim tName As String 'used as lower case....
Dim tString As String
1 Set tTLI = New TLIApplication
'... if True, we are setting else we are getting
2 tInvoke = IIf(bEscape, VbGet, VbLet)
3 If obj Is Nothing Then Exit Function
4 For Each tMem In TLI.InterfaceInfoFromObject(obj).Members
5 'tName = LCase$(tMem.Name)
6 If tMem.InvokeKind = tInvoke Then 'And tMem.Parameters.Count = 0
'could be object/something else that can't handle
On Error Resume Next
' get the oobject property value
7 tString = CallByName(obj, tMem.Name, VbGet)
8 If tInvoke = VbGet Then
9 If IndexOf(tString, "'") > 0 Then
10 tString = Replace$(tString, "'", "`")
11 CallByName obj, tMem.Name, VbLet, tString
End If
Else
'... set data replacing aposthrophe
12 If IndexOf(tString, "'") > 0 Then
13 tString = Replace$(tString, "`", "'")
14 CallByName obj, tMem.Name, VbLet, tString
End If
End If
'Debug.Print tName, " = ", tString
On Error GoTo EscapeWildCards_Err
End If
Next
Exit Function
EscapeWildCards_Err:
ErrReport Err.Description, "modCommon.EscapeWildCards", Erl
Resume Next
End Function
When I test the code in the IDE, I don't get any errors. But when I compile and test as an EXE, I get the following errors:
Object doesn't support this action. LineNo 4
Object variable or With block variable not set. LineNo 5
Object variable or With block variable not set. LineNo 6
For loop not initialized. LineNo 14
Why don't I get any errors when the app is running in the IDE, but I do when it is compiled? Can someone please point me to what am doing wrong?
First of all, you aren't helping yourself by your error handling. Doing a Resume Next without doing some form of checking the actual error is a one-stop way to confusion and bug city. Your title is misleading - it's not the "For Each" statement which is causing the problem. It is:
TLI.InterfaceInfoFromObject(obj).Members
To be precise, it is Members property which is failing. (Oh, and you obviously haven't done a straight copy and paste, because I assume that "TLI" should be "tTLI".). The reason for this is probably that the object you are trying to use does not have a public interface registered in a Type Library.
My guess is that you are trying to do this with an internal VB class, such as a Form or a private Class. At run-time, the VB IDE creates run-time type library information on the fly (how do you think you can use uncompiled DLL projects at run-time?). Whilst in the IDE, VB creates type lib info for your class, on the fly. But when compiled, this information does not exist, thus the error.
If this is the case, you will have to create interfaces for your private classes manually by creating a DLL which exposes the interface you want to use. This interface would then be implemented into the private class. Sadly, you can't do this with Form instances.

Cannot use parentheses when calling a Sub Error 800A0414 VBS

I am getting the 800A0414 error in lines 7 and 12 of this script:
Module Module1
Dim p
Sub Main()
CreateObject("Wscript.Shell").Run("program.bat", 0, True)
p = Process.GetProcessesByName("program")
If p.Count > 0 Then
WScript.Sleep(300000)
Else
CreateObject("Wscript.Shell").Run("program clean up.bat", 0, True)
End If
End Sub
Private Function WScript() As Object
Throw New NotImplementedException
End Function
End Module
I am trying to run a batch script, that starts a process, then wait until the process terminates, then run another batch script. I also do not want any command boxes being shown. If their is a easier way please let me know.
Thanks for your help
When you enclose a procedure's argument list in parentheses, you must use the Call keyword:
Call CreateObject("WScript.Shell").Run("program.bat", 0, True)
If you omit the Call keyword, you must also drop parentheses:
CreateObject("WScript.Shell").Run "program.bat", 0, True
To complete what's been said before:
When Call keyword is used to call a procedure (i.e. sub or function) the arguments must be enclosed in parentheses, except when the procedure has no arguments in which case the parentheses are optional. For example all the statements:
Call test()
Call test
Call test(1,2)
are valid, but not this one:
Call test 1
When calling a procedure without using the Call keyword, the parentheses can only be used when either the procedure has zero or one argument or the procedure has a return value (i.e. is a function) and its value is used in the same statement. For example all the statements:
test()
test(1)
test(1,2)
a = test
a = test(1,2)
a = test(test(1,2),2)
are valid, except the third one which has more than one argument. In case it's not clear, the inner call of "test" in the last statement is valid because its return value is used as an argument to another call.
Note that whenever parentheses is used in this text, it is meant to imply the possible comma-separated values as well.
Seems to me this is a VB.NET, not VBScript code.
You have Shell function in VB.NET (and other methods).
Anyway, Run returns any error code returned by the program, and if you
store that result in a variable, you can use parentheses in this case.
Dim lResult As Long
lResult = CreateObject("Wscript.Shell").Run("program.bat", 0, True)
The rest was answered by #Helen.

VBScript - Putting Array Element into GetElementById

I am writing a VBScript that automatically interacts with some web pages. I am having trouble at the final step where the script needs to click on a link to make a booking. The link for each time will only be available if that time is free. The idea of my code is to simply select the first time available (I originally though I could do this by using Mid() and GetElementId as I know the first 7 chars of each link ID but couldn't get this working). The array contains the IDs for all possible times available in a day. Some will already have been taken so that ID will no longer exist on the form.
I have 2 problems:-
1) Neither getElementBy Id or the Document.All.Item().Click commands will accept an element from the array - I get an Object Required run time error.
2) If getElementId doesn't find a matching ID it simply throws an Object required error. I wasn't expecting this, I thought that my elem variable would be nothing or null and that I could test for this.
Can anyone give me any pointers?
'This is a shortened version of my array- there are lots more times!
Times(0)="bookBtn0810"
Times(1)="bookBtn0818"
Times(2)="bookBtn0826"
Dim TimeAvail
Dim i
Dim elem
TimeAvail = "No"
i = 0
Do While (TimeAvail = "No") or (i<3)
Set elem = IE.Document.GetElementById(Chr(34) & Times(i) & Chr(34)) 'Chr(34) is to add ""
if elem is nothing then
TimeAvail = "No"
i=i+1
else
TimeAvail = "Yes"
IE.Document.All.Item(Chr(34) & Times(i) & Chr(34)).click
end if
Loop
Now, unless I'm being very silly, you won't be able to sit a variable to a non-existent element.
The only thing I can think of is to add:
On Error Resume Next
At the beginning, so it skips the error message. You may need to handle the error separately yourself.

Resources