Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB Script not so easy to figure out

Status
Not open for further replies.

ddelk

Programmer
May 28, 2003
47
0
0
US
I guess Im just missing something.

1. I can use a text stream to pull in code from a text file that contains a procedure.
2. I can then use the script control .addcode and .run to run the code.
3. Is this all there is to it? I keep reading about adding objects but when I do <scriptcontrol>.addobject "Message Box", msgbox
I get an error that msgbox cannot be used without parameters.

All Im trying to do is make the msgbox object available to the script. What am I doing wrong?
 
1) MsgBox isn't an object, it's a function
2) VBScript already supports the MsgBox function

 
It can seem a bit murky at times.

In VB6 MsgBox() is a method of the global namespace object VBA. In a sense it is invoked as:

[tt]lngResult = VBA.MsgBox(strPrompt, ...)[/tt]

Because the VBA object is part of the global namespace though you generally don't need to mention it explicitly.

VBScript's MsgBox() is an intrinsic function with pretty much identical behavior in normal use. Thus VBScript can already display a MsgBox dialog with no additional help.

The ScriptControl object's method AddObject is used to add an object to the global namespace of the script you run via the control. By adding objects within your VB program to the script's global namespace you make it available to the script code.

This is how you make the VB program scriptable: you provide one or more such global objects that in turn the script can manipulate.

Here's a sample form module's code that uses VBScript to control a form by adding the form to the script's global namespace as [tt]Form[/tt]:
Code:
Option Explicit

Private Const MyScript As String = _
    "Option Explicit" & vbNewLine _
  & "Sub EnableExit()" & vbNewLine _
  & "  Form.cmdExit.Enabled = True" & vbNewLine _
  & "End Sub"
  
Private Sub cmdExit_Click()
    Unload Me
End Sub

Private Sub cmdRunScript_Click()
    ScriptControl1.Run "EnableExit"
End Sub

Private Sub Form_Load()
    With ScriptControl1
        .AddObject "Form", Form1
        .Language = "VBScript"
        .AddCode MyScript
    End With
End Sub
In this example [tt]cmdExit[/tt] is set to disabled at design time (via the IDE).

When [tt]cmdRunScript[/tt] is clicked the script is invoked, which enables the [tt]cmdExit[/tt] button. Then the button can be clicked to end the program.


One could easily add the VBA object to the script's global namespace too, but there is usually no good reason to do so.


Usually in a real program you'd have an instance of a Class you have written that gets added to the script's global namespace. The purpose is to provide a way to manipulate things back in your VB program.


Another example might be the cases of script run under the [tt]CScript.exe[/tt] or [tt]WScript.exe[/tt] script hosts. These create and add (slightly different version of) a WScript object with various methods and properties, to provide access to the environments provided by those script engines.


By using the Script Control your VB program becomes a new, custom script host. If you want your loaded script to be able to do much in terms of controlling your VB program you'll need to define and add appropriate objects having appropriate methods and properties.

Most such programs would only add one or a few objects to the script's namespace. Those objects need to represent the things you want automated via script. They can be ADO Recordsets, Collections, Forms, instances of user-writen Classes or almost any object that doesn't require data types (like UDTs) that VBScript cannot readily manipulate.
 
Im afraid my brain dumped on the msgbox thing. Thanks for reminding me of the definition for 'object'. I also appreciate the additional details on scripting. I suppose what I'm seeing is that scripting allows me to feed in code at run-time rather than have it pre-compiled in the executable. If I later need to modify the code, I can do so without recompiling the executable.
 
Yes, that's the general idea.

Think of how you use VBA to manipulate the object's Excel, Outlook, etc. expose to your VBA code. Same idea really.

You can even use VBA to do this if you have the VBA developer tools and properly license it. That set of tools even comes with an IDE you can embed and everything! Of course it isn't free and the documentation is mostly for C++ developers who want to add VBA user programming to an application.

Using VBScript this way won't cost you anything.

You may find you'll want to write some sort of VBScript editor and a way to manage and store script modules within your application. The "cheap" way is to have your users edit scripts with NotePad and store them as external files. Somebody may already have a nice VBScript/JScript editor/manager you can plug right in though. I haven't searched for such a thing but I'd be surprised if there isn't something available.

One thing that's tough with the script control is syntax checking your user's scripts. See:

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top