Application.ExecuteScriptCode

Introduced

v5.0

Description

Executes a string of script code.

Use ExecuteScriptCode when you need to generate and execute a fragment of code written in a different scripting language (for example, to run some VBScript code from JScript).

Unlike Application.ExecuteScript, which reads script code from a file, ExecuteScriptCode takes a string of script code as an argument.

C# Syntax

Object Application.ExecuteScriptCode( String in_ScriptText, String in_LanguageProgID, String in_ProcName, Object& io_Params );

Scripting Syntax

oVariant = Application.ExecuteScriptCode( Code, Language, [ProcName], [Params] );

Return Value

A Variant containing the return value of the subroutine. The return value is always empty if no subroutine argument is specified.

Parameters

Parameter Type Description
Code String A string that contains valid script code.
Language String Specifies the scripting language of the code (for example, "VBScript", "JScript", or "Python").
ProcName String Name of a procedure (a function or subroutine) to execute. When you execute a procedure, any global code (code that is outside of a procedure) is also executed.

Tip: In VBScript, you can execute a subroutine ("sub") or a function ("function"). In JScript, procedures are declared with the "function" keyword; in Python, with "def", and in PerlScript, with "sub".
Params Array of Variants Arguments to pass to the procedure.

Examples

1. JScript Example

//JScript example that shows how to mix scripting languages
vbscriptFragment = "LogMessage \"Hello from VBScript\"" ;
pythonScriptFragment = "Application.LogMessage( \"Hello from Python\" )" ;
jscriptFragment = "LogMessage(\"Hello from JScript\")" ;
Application.ExecuteScriptCode( vbscriptFragment, "VBScript" ) ;
Application.ExecuteScriptCode( pythonScriptFragment, "Python" ) ;
// Eval is a built-in JScript function that does the same thing 
// as Application.ExecuteScriptCode, but only for JScript code
eval( jscriptFragment ) ;
// Errors in the executed script are logged and returned
// to the caller, so you can catch and handle exceptions
try
{
	Application.ExecuteScriptCode( "Bad Script Code", "VBScript" ) ;
}
catch( e )
{
	LogMessage( "Script code failed: " + e.description ) ;
}
//Expected output:
//INFO : Hello from VBScript
//INFO : Hello from Python
//INFO : Hello from JScript
//ERROR : Expected end of statement - [line 1]
//INFO : Script code failed: Expected end of statement [Line 1 in Script Text]

2. VBScript Example

'VBScript example that shows how to use mix scripting languages
vbscriptFragment = "LogMessage ""Hello from VBScript"" "
pythonScriptFragment = "Application.LogMessage( ""Hello from Python"" )" 
jscriptFragment = "LogMessage(""Hello from JScript"")" 
' Execute is a built-in VBScript function that does the same 
' thing as Application.ExecuteScriptCode, but only for VBScript code
Execute( vbscriptFragment)
Application.ExecuteScriptCode jscriptFragment , "JScript" 
'This line fails if Python is not installed
Application.ExecuteScriptCode pythonScriptFragment, "Python" 
' Errors in the executed script are logged and returned
' to the caller, so you can catch and handle exceptions
on error resume next
Application.ExecuteScriptCode "Bad Script Code", "VBScript" 
if err <> 0 then
	LogMessage "Script code failed: " & err.description 
end if
'Expected output:
'INFO : Hello from VBScript
'INFO : Hello from Python
'INFO : Hello from JScript
'ERROR : Expected end of statement - [line 1]
'INFO : Script code failed: Expected end of statement [Line 1 in Script Text]

3. JScript Example

// JScript example that shows how to call a simple VBScript function from JScript, 
// and how to pass in arguments and get the return value
var strVBRoutine = "function VBRound( in_var )\n" + 
			"VBRound = Round( in_var )\n" + 
			"end function" ;
var aArgs = new Array ;
aArgs[0] = 1.056879 ;
LogMessage( Application.ExecuteScriptCode( strVBRoutine, "VBScript", "VBRound", aArgs) ) ;
aArgs[0] = 99.9999 ;
LogMessage( Application.ExecuteScriptCode( strVBRoutine, "VBScript", "VBRound", aArgs) ) ;
// Expected results;
//INFO : 1
//INFO : 100

See Also

Command Application.ExecuteScript