openoffice-calclibreoffice-basicopenoffice-basic

How to run a script from another document


I have two doc LibreOffice calc test1.ods and test2.ods. I want run a script in test2 from the script of test1.

  1. When test2 is open by the script of test1, i can't run the script on test2 even from test2. How i can pass out the security for the test2 doc ?
  2. How i can run automaticly the scrip on test2 from the script on test1 ?

REM  *****  BASIC  *****
'Script on Test1
Sub Main

Dim urlDest As String
Dim PropFich(1) As New com.sun.star.beans.PropertyValue
Dim oDocDest As Object

    PropFich(0).Name = "MacroExecutionMode" 
    PropFich(0).value = com.sun.star.document.MacroExecMode.ALWAYS_EXECUTE
    urlDest = ConvertToUrl("/home/patrick/Bureau/Test/test2.ods")
    oDocDest = StarDesktop.loadComponentFromUrl(urlDest, "_blank", 0, PropFich())
    ' ...
    'run oDocDest.Standard.Module1.Main() ???
End Sub


Solution

  • Get the script provider of the other document and use it to invoke the macro.

    oScriptProvider = oDocDest.getScriptProvider()
    oScript = oScriptProvider.getScript(_
        "vnd.sun.star.script:Standard.Module1.Main?language=Basic&location=document")
    result = oScript.invoke(Array(), Array(), Array())
    

    For this to work, you need to allow macros to run by going to Tools > Options > Security.