run execute invoke macro Josef Frysak How to run a macro out of another macro?

The "DispatchHelper" service is capable of calling other macros registered

in Open Office. Use the "XDispatchProvider" interface of the Desktop as

environment for the call.

For further details see http://wi.wu-wien.ac.at/rgf/diplomarbeiten/BakkStuff/2008/200809_Frysak/200809_Frysak_Automating_OOo_ooRexx_Nutshells.pdf.

WARNING: The example code contains 3 scripts, which need to be stored as

different macros!

-- try to get a script context, will be .nil, if script was not invoked by OOo x_ScriptContext = uno.getScriptContext() if (x_ScriptContext <> .nil) then do -- invoked by OOo as a macro -- get context x_ComponentContext = x_ScriptContext~getComponentContext -- get desktop (an XDesktop) x_Desktop = x_ScriptContext~getDesktop -- get current document x_Document = x_ScriptContext~getDocument end else do -- called from outside of OOo, create a connection -- connect to Open Office and get component context x_ComponentContext = UNO.connect() -- create a desktop service and its interface service = "{%see com.sun.star.frame.Desktop}" s_Desktop = x_ComponentContext~getServiceManager~{%see com.sun.star.lang.XMultiServiceFactory%XMultiServiceFactory}~createInstance(service) x_Desktop = s_Desktop~{%see com.sun.star.frame.XDesktop%XDesktop} -- get the last active document x_Document = x_Desktop~getCurrentComponent() end -- this macro just works externally, called by rexxj or rexx -- create DispatchHelper service and query its interface x_MultiServiceFactory = x_ComponentContext~getServiceManager()~{%see com.sun.star.lang.XMultiServiceFactory%XMultiServiceFactory} s_DispatchHelper = x_MultiServiceFactory~createInstance("{%see com.sun.star.frame.DispatchHelper}") x_DispatchHelper = s_DispatchHelper~{%see com.sun.star.frame.XDispatchHelper%XDispatchHelper} -- get dispatch provider interface of current Desktop x_DispatchProvider = x_Desktop~{%see com.sun.star.frame.XDispatchProvider%XDispatchProvider} -- define ooRexx dispatch target MacroURL = "vnd.sun.star.script:BakkMacros.x_Sample.rex?language=ooRexx&location=user" -- prepare parameters parameters = uno.CreateArray(.UNO~PROPERTYVALUE, 2) -- traget script ignores argument names, use any name you want parameters[1] = uno.createProperty("arg1", 5) parameters[2] = uno.createProperty("arg2", 2) -- make dispatch call -- ATTENTION! do not use .nil here, instead use .uno~noProps if no parameters submitted !!! -- i.e: x_DispatchHelper~executeDispatch(x_DispatchProvider, MacroURL, "", 0, .uno~noProps) r = x_DispatchHelper~executeDispatch(x_DispatchProvider, MacroURL, "", 0, parameters) .bsf.dialog~messageBox("Result of x_Sample.rex: " || r~result, "IT Works", "information") -- define Star Basic dispatch target MacroURL = "vnd.sun.star.script:BakkMacros.x_Sample.addition?language=Basic&location=application" -- r = x_DispatchHelper~executeDispatch(x_DispatchProvider, MacroURL, "", 0, .uno~noProps) r = x_DispatchHelper~executeDispatch(x_DispatchProvider, MacroURL, "", 0, parameters) .bsf.dialog~messageBox("Result of x_Sample.addition (Star Basic Macro): " || r~result, - "IT Works", "information") ::requires UNO.CLS The called ooRexx macro (x_Sample.rex): -- a small test macro to test the x_RunMacro.rex macro info = "Adding: " || ARG(1) || " + " || ARG(2) || " using ooRexx" .bsf.dialog~messageBox(info, "IT Works", "information") return (ARG(1) + ARG(2)) ::requires BSF.CLS The called Basic macro (Store to "My Macros", Library name: "BakkMacros", Module: "x_Sample"): REM ***** BASIC ***** Sub Main RunMacro End Sub Function addition(arg1, arg2 as Integer) as Integer ' view that we are currently using Star Basic MsgBox("Adding: " & arg1 & " + " & arg2 & " using Star Basic", 64, "IT Works") ' return calculation ' to calculate make sure the parameters are Integers addition = CInt(arg1) + CInt(arg2) End Function Sub RunMacro ' create the Dispatcher service oDisp = createUnoService("com.sun.star.frame.DispatchHelper") Automating Open Office – ooRexx Nutshells Page 29 ' prepare parameters as array Dim a(1) As New com.sun.star.beans.PropertyValue a(0).Name = "arg1" : a(0).Value = 7 a(1).Name = "arg2" : a(1).Value = 1 ' macro URL to addition function above sMacroURL = "vnd.sun.star.script:BakkMacros.x_Sample.addition?language=Basic&location=application" ' call addition function r = oDisp.executeDispatch(StarDesktop, sMacroURL, "", 0, a()) ' view result MsgBox("Result of x_Sample.addition: " & r.result, 64, "IT Works") ' macro URL to x_Sample.rex sMacroURL = "vnd.sun.star.script:BakkMacros.x_Sample.rex?language=ooRexx&location=user" ' call x_Sample.rex and use the same parameters again r = oDisp.executeDispatch(StarDesktop, sMacroURL, "", 0, a()) ' show result MsgBox("Result of x_Sample.addition: " & r.result, 64, "IT Works") End Sub
Initial version