Deciding when to use VBscript and when to use MS script

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
billtubbs
Junior Coder
Posts: 32
Joined: Thu Dec 07, 2006 10:00 pm
Location: Vancouver
Contact:

Deciding when to use VBscript and when to use MS script

Post by billtubbs » Thu Mar 15, 2007 11:09 pm

Hi

I need to think through my strategy carefully for writing a macro that deals with quite a few variables and some fairly large matrices (2D). I will need to use VBscript functions but I want the overall control to remain in Macro scheduler script. I've done some research and I think I should use the following guidelines to decide what to put in VB script and what to leave in macro scheduler script.

VB script:
- database access
- anything using arrays/complex math

MS script
- automation of applications
- file I/O
- user interface

My big problem is that file I/O does not seem to be possible in VB script. Is this true? I searched the VB script language reference at msdn.microsoft.com/ and found nothing on this.

This is a problem because without it I will have to transfer a lot of values to VB script and back to MS repeatedly. I've looked into this and as far as I can tell you can only send variables to VB script as arguments to functions and return them in a single argument from functions (or a few variables in a delimited text string). If all my VB script functions use a common set of initialised variables do I have to transfer the whole lot every time I call a function? And to return the contents of an array do I have to call the VB script function to return one data point at a time?

Please advise if you know a better strategy for this.

Bill.

billtubbs
Junior Coder
Posts: 32
Joined: Thu Dec 07, 2006 10:00 pm
Location: Vancouver
Contact:

Post by billtubbs » Thu Mar 15, 2007 11:49 pm

Here's one tip I thought of myself already:

If I declare Public variables for all variables commonly used in VB script then I can run an initialising sub-routine once at the beginning that passes all the values from Macro script. After this, only variables that change need to be passed to VB script with each function call.

Still would like to know if file access possible in VB script.

User avatar
Captive
Macro Veteran
Posts: 213
Joined: Sun Oct 20, 2002 8:37 pm
Location: Colorado, USA

Post by Captive » Fri Mar 16, 2007 3:13 am

You can declare a VBScript "global" array or variable, and use it from any of your VBScript routines. This can also be a multi-dimensional array.

Example;

VBSTART

Dim aFiles()
Dim nFileCount

Function blahfunction (param)

Dim fso, oFolder, sResult

' etc
' You can here refer or change the aFiles array or nFileCount variable

blahfuncton = sResult
End Function

VBEND


As for file access, vbscript can use FilesSystemObject, which is fairly good for text based procedures.

For quick reference, I prefer to use the downloadable VBScript reference (script56.chm), linked via http://www.mjtnet.com/resources.htm as "download the latest documentation". This lists examples for usage with the FSO, and procedures and properties that comes with.

billtubbs
Junior Coder
Posts: 32
Joined: Thu Dec 07, 2006 10:00 pm
Location: Vancouver
Contact:

Post by billtubbs » Fri Mar 16, 2007 7:08 pm

OK, so all my variables are now VB script global variables. That works.

Now to change any of them from MS script I have made subroutines such as this one:

Code: Select all

Sub setx(P1_c, P2_c, P3_c, PA_c, TEXP_c)

P1=P1_c
P2=P2_c
P3=P3_c
PA=PA_c
TEXP=TEXP_c

MsgBox("P1 set to "&P1)

End Sub
Problem is I can't find a way to call the VB subroutine with variable values. I tried the following:

VBRun>setx,%P1%,%P2%,%P3%,%PA%,%TEXP%

and the VB script sub-routine reported:

P1 set to %P1%

I know how to call a VB function using VBEval but struggling to call a subroutine and pass variables over.

Bill.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri Mar 16, 2007 8:03 pm

VBRun>Setx,1,5,2,whatever...
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

billtubbs
Junior Coder
Posts: 32
Joined: Thu Dec 07, 2006 10:00 pm
Location: Vancouver
Contact:

Post by billtubbs » Fri Mar 16, 2007 9:11 pm

Thanks, but your suggestion only shows how to pass constants (1,2,3). I want to pass an MS variable 'P1' to the VB script subroutine.

For now I have converted the subroutine to a VB function and I am passing the variables as follows:

Code: Select all

VBEval>setx(%P1%,%P2%,%P3%,%PA%,%TEXP%),result
This works, but surely you can pass a non-constant to a subroutine as well?

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri Mar 16, 2007 9:15 pm

Of course:

Let>P1=2
Let>P2=3
Let>P3=..
..
VBRun>SetX,P1,P2...
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts