שתף באמצעות


how to use microsoft script control in vb.net

Question

Friday, February 3, 2012 1:05 AM

I would like to execute vbscript code in vb.net using the microsoft script control (msscript.ocx).  Whats the best way to do this?

theirs an executestatement or addcode method i can use which one should i? I've been trying to convert my vbscript to vb.net but its too complex even though its not big and thought this control might help me save time. 

Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - "Sherlock holmes" "speak softly and carry a big stick" - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda

All replies (4)

Friday, February 3, 2012 7:31 PM ✅Answered | 1 vote

The 2 methods are different, AddCode and Run method work together while ExecuteStatement works with AddObject method. So when you use AddCode, Run method must be called to execute the function you passed to AddCode Method.

 AddCode/Run : For example, if want to run the script function/method below using Msscriptcontrol from Vb.NEt

 

VBScript to run

Sub Hello

MsgBox "Hello, MsScriptControl"

End Sub

In VB.NET, you can declare it as variable or enter it in textbox

Private myScript As New MSScriptControl.ScriptControlClass

 

 

Dim ScriptToRun As String = "Sub HeLLo" & vbCrLf & "MsgBox ""Hello, MsScriptControl""" & vbCrLf & "End Sub" & vbCrLf

Now, execute the script above with code in button1 click event

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

** myScript = New MSScriptControl.ScriptControlClass**

**        myScript.Language = "VBScript"       **

**        myScript.AddCode(ScriptToRun) 'Add script to run to addcode method**

**        myScript.Run("HeLLo")  'run Hello method     **

End Sub

 

 ==============================================

AddObject/ExecuteStatement : For example, if you want to change any property of any control include the Form itself using vbscript from your VB Application

Below code will change the top value of Button2 location

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

**  TextBox1.Text = "Button2.Top=0"  **

**  myScript = New MSScriptControl.ScriptControlClass**

**  myScript.Language = "VBScript"**

**  myScript.AddObject("Button2", Button2, True)**

**  myScript.ExecuteStatement(TextBox1.Text)**

**  myScript = Nothing    **

**End Sub **

I hope this helps, also check out this link on how to use addcode method to execute script http://drdobbs.com/184405844

kaymaf

 

CODE CONVERTER SITE

http://www.carlosag.net/Tools/CodeTranslator/.

http://www.developerfusion.com/tools/convert/csharp-to-vb/.


Friday, February 3, 2012 7:40 PM

Im wondering about if options for executing a wsh script  file (xml version of vbscript) like script control executes vbscript wheter it be a library or control im wondering if their is one for executing code inside of my vb.net app?Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - "Sherlock holmes" "speak softly and carry a big stick" - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda


Saturday, February 4, 2012 1:29 AM

Im wondering about if options for executing a wsh script  file (xml version of vbscript) like script control executes vbscript wheter it be a library or control im wondering if their is one for executing code inside of my vb.net app? Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - "Sherlock holmes" "speak softly and carry a big stick" - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda

As far as i know, it only execute vbscript and jscript

kaymaf

CODE CONVERTER SITE

http://www.carlosag.net/Tools/CodeTranslator/.

http://www.developerfusion.com/tools/convert/csharp-to-vb/.


Saturday, February 4, 2012 2:17 AM

Yeah i didnt know powershell could execute vbscript using scriptcontrol com component but theirs a lot of samples of it being done and powershell can be used inside a vb.net application.Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - "Sherlock holmes" "speak softly and carry a big stick" - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda