AMS: How To Execute A VBScript Function In AMS
In this short tutorial i will show you how to run a VBScript in AMS
VBScript engine has a COM interface to access Microsoft Script Engine
with a ProID called 'ScriptControl'
In AMS we can use LuaCom plugin to access this COM interface and execute our VBScript functions
you can pass a argument to VBScript function and get return value easiliy
there is a limitation , this limitation comes from LuaCom plugin about SAFEARRAY
but this can not inhibit our implementation
basicly we can pass only one argument to our VBScript function , but we can use a delimited string to
avoid this limitation
we will use :
instead of
This is our test VBScript function
like you can see above i used Split(strInput,"|",-1,1) function of VBScript to split input argument
to multiple arguments with a separator i will use in AMS "|" , the separator character can be anything you prefer
This is our caller procedure in AMS :
where VBScriptTestFunc is the name of our VBScript function name
and second argument contains a delimited string to be passed to our VBScritp function
then we will split it in VBScript function
Finally , this is full sample of the tutorial that can be tested by adding it to a buttons On Click event
Note : you should have LuaCom Plugin installed to test/run this code
if not installed then please download it from here and install
In this short tutorial i will show you how to run a VBScript in AMS
VBScript engine has a COM interface to access Microsoft Script Engine
with a ProID called 'ScriptControl'
In AMS we can use LuaCom plugin to access this COM interface and execute our VBScript functions
you can pass a argument to VBScript function and get return value easiliy
there is a limitation , this limitation comes from LuaCom plugin about SAFEARRAY
but this can not inhibit our implementation
basicly we can pass only one argument to our VBScript function , but we can use a delimited string to
avoid this limitation
we will use :
ScriptControl:Run("VBScriptTestFunc","Test Argument 1|Test Argument 2|Test Arguement 3");
instead of
ScriptControl:Run("VBScriptTestFunc","Test Argument 1","Test Argument 2","Test Arguement 3");
This is our test VBScript function
Function VBScriptTestFunc(strInput)
strArgs = Split(strInput,"|",-1,1)
NCount = 1
for each Argument in strArgs
msgbox("Argument "& NCount &" : "& Argument)
NCount = NCount + 1
next
VBScriptTestFunc = "This Is Result Of VBScript"
End Function
like you can see above i used Split(strInput,"|",-1,1) function of VBScript to split input argument
to multiple arguments with a separator i will use in AMS "|" , the separator character can be anything you prefer
This is our caller procedure in AMS :
local ScriptControl = luacom.CreateObject("ScriptControl");
if ScriptControl then
ScriptControl:setLanguage("VBScript");
ScriptControl:setAllowUI(true);
ScriptControl:setSitehWnd(Application.GetWndHandle());
ScriptControl:AddCode(strVBSCript);
retval = ScriptControl:Run("VBScriptTestFunc","Test Argument 1|Test Argument 2|Test Arguement 3");
Dialog.Message("Notice", retval, MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);
ScriptControl = nil;
collectgarbage()
end
and second argument contains a delimited string to be passed to our VBScritp function
then we will split it in VBScript function
Finally , this is full sample of the tutorial that can be tested by adding it to a buttons On Click event
Note : you should have LuaCom Plugin installed to test/run this code
if not installed then please download it from here and install
strVBSCript = [[
Function VBScriptTestFunc(strInput)
strArgs = Split(strInput,"|",-1,1)
NCount = 1
for each Argument in strArgs
msgbox("Argument "& NCount &" : "& Argument)
NCount = NCount + 1
next
VBScriptTestFunc = "This Is Result Of VBScript"
End Function
]]
local ScriptControl = luacom.CreateObject("ScriptControl");
if ScriptControl then
ScriptControl:setLanguage("VBScript");
ScriptControl:setAllowUI(true);
ScriptControl:setSitehWnd(Application.GetWndHandle());
ScriptControl:AddCode(strVBSCript);
retval = ScriptControl:Run("VBScriptTestFunc","Test Argument 1|Test Argument 2|Test Arguement 3");
Dialog.Message("Notice", retval, MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);
ScriptControl = nil;
collectgarbage()
end