首页 > 解决方案 > 如何从另一个文档运行脚本

问题描述

我有两个文档 LibreOffice calc test1.ods 和 test2.ods。我想从 test1 的脚本在 test2 中运行一个脚本。

  1. 当 test2 由 test1 的脚本打开时,即使从 test2 也无法在 test2 上运行脚本。我如何才能通过 test2 doc 的安全性?
  2. 如何从 test1 上的脚本自动运行 test2 上的脚本?

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

标签: openoffice-calclibreoffice-basicopenoffice-basic

解决方案


获取另一个文档的脚本提供程序并使用它来调用宏。

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

为此,您需要通过转到Tools > Options > Security来允许运行宏。


推荐阅读