Application.ExecuteScriptCode

導入

v5.0

詳細

スクリプトコードの文字列を実行します。

異なるスクリプト言語で記述されたコードの断片を生成、実行する必要がある場合には ExecuteScriptCode を使用します(Jscript から VBScript コードを実行する場合など)。

Application.ExecuteScriptファイルからスクリプトコードを読み取るとは異なり、ExecuteScriptCode はスクリプトコードを引数として取得します。

スクリプト 構文

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

戻り値

サブルーチンの戻り値を含むVariantサブルーチン引数を指定しない場合、この戻り値は常に空です。

パラメータ

パラメータ タイプ 詳細
コード String 有効なスクリプトコードを含む文字列。
言語 String コードのスクリプト言語を指定します(VBScript、Jscript、Python など)。
ProcName String 実行するプロシージャ(関数やサブルーチン)の名前。プロシージャを実行する際には、任意のグローバルコード(プロシージャの外部にあるコード)も実行されます。

VBScript スクリプトでは、サブルーチン("sub")または関数("function")を実行できます。JScript では"function"キーワード、Python では"def"、Perlでは"sub"でプロシージャをそれぞれ宣言します。
Params VariantArray プロシージャに渡す引数。

1. JScript の例

//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 の例

'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 の例

// 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

関連項目

Command Application.ExecuteScript