I created a "Test" SUB/macro using the OOBasic IDE in OOoCalc. I created the Sub in a new library "MyOOolib" inside "Module1". When called it will show a dialog "hello World" . I now wish to call it from an external application. I wrote the following code in my external application to invoke the script from my application
Approach 1:
Code: Select all
rComponentContext = defaultBootstrap_InitialComponentContext();
Reference< XMultiComponentFactory > xMultiComponentFactoryClient = rComponentContext->getServiceManager();
// Get the rOfficeServiceManager using ooConnect() from the SdK samples.
// Query the XDispatcher Interface
Reference< XDispatchHelper > rDispatchHelper = Reference< XDispatchHelper > ( rOfficeServiceManager->createInstance(
OUString( RTL_CONSTASCII_USTRINGPARAM(
"com.sun.star.frame.DispatchHelper" ))), UNO_QUERY );
// Query the XDispatchProvider Interface
Reference< XDispatchProvider > rDispatchProvider(rDesktop,UNO_QUERY);
rDispatchHelper->executeDispatch(rDispatchProvider,
OUString::createFromAscii("vnd.sun.star.script:MyOOoLib.Module1.Test?language=Basic&location=document"),
OUString::createFromAscii(""),
0,
Sequence < ::com::sun::star::beans::PropertyValue > ());
Second approach:
In the second approach I use the XScriptProviderFactory to invoke the script.
Code: Select all
Reference< XScriptProvider > xScriptProvider;
Reference< XScriptProviderFactory > xFactory(rComponentContext->getValueByName(
::rtl::OUString::createFromAscii( "/singletons/com.sun.star.script.provider.theMasterScriptProviderFactory" ) ),
UNO_QUERY );
if ( xFactory.is() )
{
Any aCtx;
aCtx <<= ::rtl::OUString::createFromAscii( "user" ); // I am not sure if this correct.
xScriptProvider.set( xFactory->createScriptProvider( aCtx ), UNO_QUERY );
}
if ( !xScriptProvider.is() )
{
printf("ExecuteOOoBasicCommand() Failed to create msp");
return 1;
}
Sequence< Any > inArgs( 0 );
Sequence< Any > outArgs( 0 );
Sequence< sal_Int16 > outIndex;
Reference< XScript > xScript = xScriptProvider->getScript(::rtl::OUString::createFromAscii
( "vnd.sun.star.script:MyOOoLib.Module1.Test?language=Basic&location=application" ));
if ( !xScript.is() )
{
printf("ExecuteOOoBasicCommand() Failed to obtain XScript");
return 1;
}
Any result = xScript->invoke( inArgs, outIndex, outArgs );
I am not sure which is the right approach.
I am looking for some help to get this sorted.
Thanks
Mangesh