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.
Deciding when to use VBscript and when to use MS script
Moderators: Dorian (MJT support), JRL
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.
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.
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.
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.
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:
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.
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
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.
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
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?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
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:
This works, but surely you can pass a non-constant to a subroutine as well?
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
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Of course:
Let>P1=2
Let>P2=3
Let>P3=..
..
VBRun>SetX,P1,P2...
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?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?