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!

Run VBScript within VFP 3

Status
Not open for further replies.

craigsboyd

IS-IT--Management
Nov 9, 2002
2,839
0
0
US
Just a quick and dirty way to execute VBScript or JScript (Javascript) from within VFP using the Microsoft Script Control. Opens up some possibilities for extending VFP. Below is a very simple example using VBScript. I kept it simple on purpose so members could easily see the basic way this works. Cut-n-paste the code below into a prg file and run it from within VFP.

Code:
Local lcBegin, lcEnd, loScript, lcVBScriptCode
lcBegin = [Sub ShowMyMessage] + Chr(10) + Chr(13)
lcVBScriptCode = [MsgBox "VBScript was just executed and this is the resulting message box!"]
lcEnd= Chr(10) + Chr(13) + [ End Sub]
lcVBScriptCode = lcBegin + lcVBScriptCode + lcEnd
loScript = Createobject([MSScriptcontrol.scriptcontrol.1])
loScript.Language = [VBScript]
loScript.AddCode(lcVBScriptCode)
loScript.executestatement("ShowMyMessage")

Slighthaze = NULL

[ul][li]FAQ184-2483
An excellent guide to getting a fast and accurate response to your questions in this forum.[/li][/ul]
 
So now let's see how this can be used further (Cut-n-paste the code below into a prg file and run it from within VFP).


PUBLIC oForm
oForm = CREATEOBJECT("clsscriptform")
oForm.show()

DEFINE CLASS clsscriptform AS form

Autocenter = .T.
Top = 0
Left = 0
Height = 512
Width = 443
DoCreate = .T.
Caption = "Form1"
Name = "Form1"

ADD OBJECT shape1 AS shape WITH ;
Top = 336, ;
Left = 65, ;
Height = 113, ;
Width = 313, ;
BackStyle = 0, ;
SpecialEffect = 0, ;
Name = "Shape1"

ADD OBJECT text1 AS textbox WITH ;
Height = 23, ;
Left = 76, ;
Top = 353, ;
Width = 288, ;
Name = "Text1"

ADD OBJECT command1 AS commandbutton WITH ;
Top = 252, ;
Left = 30, ;
Height = 27, ;
Width = 84, ;
Caption = "Execute", ;
Name = "Command1"

ADD OBJECT edit1 AS editbox WITH ;
Height = 199, ;
Left = 29, ;
Top = 41, ;
Width = 385, ;
Name = "Edit1"

ADD OBJECT optiongroup1 AS optiongroup WITH ;
ButtonCount = 2, ;
Value = 1, ;
Height = 46, ;
Left = 76, ;
Top = 389, ;
Width = 144, ;
Name = "Optiongroup1", ;
Option1.Caption = "Option1", ;
Option1.Value = 1, ;
Option1.Height = 17, ;
Option1.Left = 5, ;
Option1.Top = 5, ;
Option1.Width = 61, ;
Option1.Name = "Option1", ;
Option2.Caption = "Option2", ;
Option2.Height = 17, ;
Option2.Left = 5, ;
Option2.Top = 24, ;
Option2.Width = 61, ;
Option2.Name = "Option2"

ADD OBJECT command2 AS commandbutton WITH ;
Top = 389, ;
Left = 256, ;
Height = 27, ;
Width = 84, ;
Caption = "Command2", ;
Name = "Command2"

ADD OBJECT label1 AS label WITH ;
AutoSize = .T., ;
BackStyle = 0, ;
Caption = ['"MyForm" references Thisform'], ;
Height = 17, ;
Left = 255, ;
Top = 257, ;
Width = 167, ;
Name = "Label1"

ADD OBJECT label2 AS label WITH ;
AutoSize = .T., ;
BackStyle = 0, ;
Caption = "Some controls to mess with through VBScript", ;
Height = 17, ;
Left = 64, ;
Top = 317, ;
Width = 249, ;
Name = "Label2"

ADD OBJECT label3 AS label WITH ;
AutoSize = .T., ;
BackStyle = 0, ;
Caption = "Write your VBScript below and click Execute", ;
Height = 17, ;
Left = 29, ;
Top = 17, ;
Width = 238, ;
Name = "Label3"

PROCEDURE Init
thisform.edit1.Value = [MsgBox ("I am about to change the controls below through VBScript")] + CHR(13) + CHR(10) + ;
[MyForm.Text1.value = "Changed It!"]+ CHR(13) + CHR(10) + ;
[MyForm.Optiongroup1.value = 2]+ CHR(13) + CHR(10) + ;
[MyForm.Optiongroup1.option1.Caption = "Male"]+ CHR(13) + CHR(10) + ;
[MyForm.Optiongroup1.option2.Caption = "Female"]+ CHR(13) + CHR(10) + ;
[MyForm.Command2.caption = "Testing"]+ CHR(13) + CHR(10) + ;
[MsgBox ("Now write your own VBScript in the Editbox provided")]
ENDPROC

PROCEDURE command1.Click
Local loScript
loScript = Createobject([MSScriptcontrol.scriptcontrol.1])
loScript.Language = [VBScript]
loscript.addobject("MyForm", thisform)
loScript.executestatement(thisform.edit1.value)
ENDPROC

ENDDEFINE


Slighthaze = NULL

[ul][li]FAQ184-2483
An excellent guide to getting a fast and accurate response to your questions in this forum.[/li][/ul]
 
Thank you for this code.
I kept it for later use.


Andy Rice
San Diego, CA
 
Slighthaze,

Very good code.
I also kept it for later use.
But, have you got any example for Javascript ? I would appreciate that.

Tks.
CCostaBr
 
another cool piece of code! simple and yet clever. and i think it's very useful too.

kilroy [thumbsup2]
 
Here is the JavaScript example. You may notice when you run it that I don't bring up any message boxes using "alert" and the reason behind that is Alert is actually part of the IE object model (window.alert) and so it doesn't work when hosted by the Script control. So we just do some simple string stuff and a quick math calculation here instead and show the results via the controls on the form. Now, before too long here I will provide some much more complex examples of using this technology in VFP. But for now:

PUBLIC oForm
oForm = CREATEOBJECT("clsscriptform")
oForm.show()

DEFINE CLASS clsscriptform AS form

Autocenter = .T.
Top = 0
Left = 0
Height = 512
Width = 443
DoCreate = .T.
Caption = "JavaScript Example"
Name = "Form1"

ADD OBJECT shape1 AS shape WITH ;
Top = 336, ;
Left = 65, ;
Height = 113, ;
Width = 313, ;
BackStyle = 0, ;
SpecialEffect = 0, ;
Name = "Shape1"

ADD OBJECT text1 AS textbox WITH ;
Height = 23, ;
Left = 76, ;
Top = 353, ;
Width = 288, ;
Name = "Text1"

ADD OBJECT command1 AS commandbutton WITH ;
Top = 252, ;
Left = 30, ;
Height = 27, ;
Width = 84, ;
Caption = "Execute", ;
Name = "Command1"

ADD OBJECT edit1 AS editbox WITH ;
Height = 199, ;
Left = 29, ;
Top = 41, ;
Width = 385, ;
Name = "Edit1"

ADD OBJECT optiongroup1 AS optiongroup WITH ;
ButtonCount = 2, ;
Value = 1, ;
Height = 46, ;
Left = 76, ;
Top = 389, ;
Width = 144, ;
Name = "Optiongroup1", ;
Option1.Caption = "Option1", ;
Option1.Value = 1, ;
Option1.Height = 17, ;
Option1.Left = 5, ;
Option1.Top = 5, ;
Option1.Width = 61, ;
Option1.Name = "Option1", ;
Option2.Caption = "Option2", ;
Option2.Height = 17, ;
Option2.Left = 5, ;
Option2.Top = 24, ;
Option2.Width = 61, ;
Option2.Name = "Option2"

ADD OBJECT command2 AS commandbutton WITH ;
Top = 389, ;
Left = 256, ;
Height = 27, ;
Width = 84, ;
Caption = "Command2", ;
Name = "Command2"

ADD OBJECT label1 AS label WITH ;
AutoSize = .T., ;
BackStyle = 0, ;
Caption = ['"MyForm" references Thisform'], ;
Height = 17, ;
Left = 255, ;
Top = 257, ;
Width = 167, ;
Name = "Label1"

ADD OBJECT label2 AS label WITH ;
AutoSize = .T., ;
BackStyle = 0, ;
Caption = "Some controls to mess with through JavaScript ", ;
Height = 17, ;
Left = 64, ;
Top = 317, ;
Width = 249, ;
Name = "Label2"

ADD OBJECT label3 AS label WITH ;
AutoSize = .T., ;
BackStyle = 0, ;
Caption = "Write your JavaScript below and click Execute", ;
Height = 17, ;
Left = 29, ;
Top = 17, ;
Width = 238, ;
Name = "Label3"

PROCEDURE Init
thisform.edit1.Value = [var name = "Hello World";] + CHR(13) + CHR(10) + ;
[var x = 6;] + CHR(13) + CHR(10) + ;
[var y = 12;] + CHR(13) + CHR(10) + ;
[var z = y / x;] + CHR(13) + CHR(10) + ;
[MyForm.text1.value = name;] + CHR(13) + CHR(10) + ;
[MyForm.optiongroup1.value = z;]
ENDPROC

PROCEDURE command1.Click
Local loScript
loScript = Createobject([MSScriptcontrol.scriptcontrol.1])
loScript.Language = [JScript]
loscript.addobject("MyForm", thisform)
loScript.executestatement(thisform.edit1.value)
ENDPROC

ENDDEFINE


Slighthaze = NULL

[ul][li]FAQ184-2483
An excellent guide to getting a fast and accurate response to your questions in this forum.[/li][/ul]
 
slighthaze,

Tks.

Best regards
CCostaBr
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top