Jump to content

simple problems with vbscript


Recommended Posts

Option Explicit
Const strConnection = "RAS"
Const strUsername = "UserXYZ"
Dim oShell

Set oShell = CreateObject ("WScript.Shell")

'run command and wait for it to exit
oShell.Run("rasdial.exe " & strConnection & " " & strUsername, 1, true)

OK, I'm new to vbscript, can somebody point out what in heck is wrong with that script? it looks good to me...

seems to be ok until WSH hits the "oShell.Run" line, there I get a compile error "no brackets allowed in subroutine call" or something like that. (german windows xp)

Link to comment
Share on other sites


Option Explicit
Const strConnection = "RAS"
Const strUsername = "UserXYZ"
Dim oShell

Set oShell = CreateObject ("WScript.Shell")

'run command and wait for it to exit
oShell.Run("rasdial.exe " & strConnection & " " & strUsername, 1, true)

OK, I'm new to vbscript, can somebody point out what in heck is wrong with that script? it looks good to me...

seems to be ok until WSH hits the "oShell.Run" line, there I get a compile error "no brackets allowed in subroutine call" or something like that. (german windows xp)

Try either one of the following:

oShell.Run "rasdial.exe" & " " & strConnection & " " & strUsername, 1, true

oShell.Run("rasdial.exe" & " " & strConnection & " " & strUsername), 1, true

Link to comment
Share on other sites

Well then remove the brackets:

oShell.Run "rasdial.exe " & strConnection & " " & strUsername, 1, true

Works. Personally I hate VBScript for this kind of (apparent?) inconsistencies. CreateObject has to have brackets, and Run should not have. My advice is to use JScript instead. You can do the same things with a much more clear syntax.

Link to comment
Share on other sites

... I get a compile error "no brackets allowed in subroutine call" or something like that. (german windows xp)

You will find that subroutines do not return a value so do not use brackets. On the use of functions with storing the returned value into a variable, then you may need to use brackets. I keep to that concept and has proven consistent with good results.

So this should be fine

subroutine "parameter"

and this

variable = function("parameter")

The below (reversed usage of brackets) will occur error that I would expect.

subroutine ("parameter")

and this

variable = function "parameter"

So if it returns a value, then use brackets, else do not. I'm not sure by my memory atm about the following, but if you do not store the value of a function return, then do not use brackets (like a subroutines behavior) to avoid error. A function may require brackets if used with an IF statement like below to also avoid error.

If function("parameter") Then

Edit:

Added last sentence about using IF statement

Edited by MHz
Link to comment
Share on other sites

I figured it out by now... brackets without storing the value = BAD.

one reason why I like AutoIt over vbscript... but for what I am working on I am trying to work with what comes in the box with Windows, since I dont want to add too much additional stuff. (Thin client system= XP embedde)

Link to comment
Share on other sites

but for what I am working on I am trying to work with what comes in the box with Windows, since I dont want to add too much additional stuff.
JScript is as available as VBScript. It's the same engine (WScript.exe or CScript.exe), only a different syntax. Your code in JScript would be:
var strConnection = "RAS";
var strUsername = "UserXYZ";
var oShell;

oShell = WScript.CreateObject ("WScript.Shell");

// run command and wait for it to exit
oShell.Run("rasdial.exe " + strConnection + " " + strUsername, 1, true);

save as .js file, and it just works.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...