You are on page 1of 3

Print Article

Seite 1 von 3

Issue Date: FoxTalk June 1998

Use the Microsoft Scripting Control to Script with VFP


Rod Paddock

Have you ever wanted to add a programming language to your Visual FoxPro applications? If you answered yes, you've come to the right place. Microsoft has released an ActiveX control for adding programming languages to your VFP applications -- the Microsoft scripting control.
Visual FoxPro supports two constructs for adding custom scripting to your applications. You can use Macro expansion -- the & operator -- or the EVAL() function. The problem with these constructs is they limit you to using simple commands and expressions. You can't use control structures like IF/ENDIF, DO/LOOP, FOR/NEXT, or calling subroutines. This is where VFP's native capabilities end and the advantages of the scripting control begins. Where to find the Microsoft scripting control The first step in using the Microsoft scripting control is to download and install it -- it can be found at www.microsoft.com/scripting. Once you've installed the scripting control, add an instance of the control to a form, and get ready to rock. A quick note about a great utility... A utility program called CodeBlock by Randy Pearson was created a few years ago to address the limitation of combining macro expansion capabilities with looping constructs like DO WHILE and FOR/NEXT loops. Essentially, Randy built a runtime interpreter for FoxPro. With the exception of those commands that aren't allowed outside the FoxPro IDE -- such as COMPILE -- just about any other block of code is allowed. The program allows you to store code in memo fields -- and have that code executed on an interpreted basis -- as if it were compiled. So if you have FoxPro or VFP applications that you'd like to equip with this capability, check out CodeBlock. A copy is included in the accompanying Download file . However, if you're interested in adding new language capabilities like VBScript or JavaScript to your VFP applications, then read on. (Tip: If you're interested in acquiring a VBScript reference, take a look at this URL: http://www.microsoft.com/scripting/default.htm?/scripting/vbscript/default.h. Or, if you already own Visual Studio, take a look at the Help files that come with Visual Interdev -- it includes VBScript and JavaScript references.) Scripting control basics Using the scripting control in its basic form only requires four steps: 1. Add an instance to a form. 2. Set the language property. Your choices of languages are VBScript or JScript. 3. Add the code you want to execute to the control using the AddCode method. 4. Execute the code using the Execute Statement method of the control. The following code creates a basic message box function using VBScript: #DEFINE CRLF chr(10) + chr(13) *-- Set the language Thisform.oleScriptingControl.Language = "VBScript" *-- Add a function Thisform.oleScriptingControl.AddCode([Function Test] ; + CRLF + [msgbox "hello"] ; + CRLF + [ End Function ]) *-- Execute the function Thisform.oleScriptingControl.ExecuteStatement("Test")

That seems pretty straightforward, doesn't it? Here's how the code works: The first step to using the scripting control is to set the controls language property. You have two choices: VBScript and JScript. If you specify the language property as VBScript, the code you add with the AddCode method must be VBScript (a subset of Visual Basic for Applications). If you specify JScript, the code added must be in JavaScript. Next, use the AddCode method to load the control with source code to execute. The AddCode method accepts one parameter: a string that contains multiple lines of code delimited by CR+LF characters. Finally, run your code using either the Run or ExecuteStatement methods. The Run method executes functions or subroutines. The ExecuteStatement method executes a single statement (like "x = 1") or a simple call to a function. There's

http://foxtalknewsletter.com/ME2/Audiences/Segments/Publications/Print.asp?Module=... 06.02.06

Print Article

Seite 2 von 3

one aspect of this method that's rather tedious -- the requirement for you to code manually, delimiting each programming line with CR+LF sequences. As a result, wouldn't it be convenient to have a mechanism similar to the VFP command window? I thought so, so I created a Scripting Control testbed. Figure 1 displays my Visual Basic command window style interface. Figure 1. The form demonstrates the capabilities of the Microsoft scripting control.

Manipulating FoxPro objects with VBScript Now for something really cool. One of the most powerful aspects of the scripting control is its ability to manipulate VFP (or Visual Basic) native objects. Calling the AddObject method of the scripting control does this. Yes, the scripting control has a method called AddObject, so you can add an object to the scripting object container. The syntax for this method consists of two parameters. The first is the name that the object will be referenced by in your VBScript or JScript code. The second parameter is a handle to your object. To add a form to the scripting control, use the following syntax: thisform.oleScriptingControl.Object.AddObject( ; "frmFoxProForm",Thisform)

One item you'll quickly notice is the inclusion of the Object keyword. VFP provides this property to deal with ActiveX controls that share similar properties and methods with VFP objects. If you attempt to call the AddObject method without the Object keyword, VFP will address the FoxPro version of the AddObject method and not the AddObject method of the scripting control. Upon adding a reference to your VFP object, you can begin addressing the object in your VBScript using the first parameter of the AddObject method. The following code demonstrates changing properties on a VFP form using VBScript: Function SetFormProperties() If IsObject(frmFoxProForm) Then frmFoxProForm.Caption = "Hello World" frmFoxProForm.BackColor = 100000 End If End Function

This example demonstrates setting properties on a VFP object. You can also call VFP methods. The following code shows how to call the click event of a button on the scripting form: Function CallFormMethod() If IsObject(frmFoxProForm) Then frmFoxProForm.BackColor = 400000 frmFoxProForm.cmdClickTest.Click() End If End Function

Scripting "gotchas" When developing applications using the Microsoft scripting control, there are some "gotchas" you need to take into account. The first deals with adding VFP objects to the scripting control using the AddObject method. It's important to make sure the scripting control releases your objects when it's done with them. You can release your VFP controls by calling the scripting control's Reset method. If you don't use the Reset method, VFP might get a visit from Dr. Watson when you release the form with the scripting control on it. The best place to put the Reset call is in the QueryUnload event of the scripting control's form. While I'm talking about Dr. Watson, there's another item to take into account. If you use ActiveX controls in your applications, you need to turn FoxPro's ActiveX Dual Interface option off. You do this by putting the following call in either your main program or in the Load method of your form: *-- Turn ActiveX Dual Interface Option Off SYS(2333,0)

If you don't do this, FoxPro won't release your forms correctly. Finally, for good measure, it's always a good idea to call the scripting control's Reset method after executing code. The reason

http://foxtalknewsletter.com/ME2/Audiences/Segments/Publications/Print.asp?Module=... 06.02.06

Print Article

Seite 3 von 3

for this is that the control has a tendency to become unstable if you call the AddCode method repeatedly. Conclusion Now that you have a basic understanding of using the scripting controls, you might say, "So what good does this do me?" Here are some uses I've already found handy:

n Add your own macro language to your applications. ( Hint: Use a memo field for storing code.) n Adopt existing Visual Basic or JavaScript code, like functions stored in Active Server Pages. n Even use an alternate language for building your FoxPro applications.

You're only limited by your creativity when it comes to adding scripting to your applications. Finally, I'd like to thank two people for assisting me with this article: Ken Levy, for inspiring it in the first place, and John Petersen, for making sure my article would be useful for other VFP developers.

http://foxtalknewsletter.com/ME2/Audiences/Segments/Publications/Print.asp?Module=... 06.02.06

You might also like