在“基本”OpenOffice编程中传递数组

时间:2014-05-15 12:46:19

标签: ubuntu-12.04 libreoffice libreoffice-basic

我被要求在工作中做点什么,而且我正在运行Ubuntu而不是Windows,我有Libre Office(LO Writer相当于Word)。 - 任务是自动化一些合同,其中文档保持90%相同,除了一些从doc更改为doc的变量。

首先 - 基本是一场噩梦,通常这整个宏写作过程也非常糟糕。

现在转到“代码” - 我不断收到有关BASIC error: Argument is not optional的错误消息 我要做的就是将两个数组传递给另一个函数:

Function test ( ByVal changeFrom() As String ,ByVal changeTo() As String  )
Dim I As Long
Dim Doc As Object
Dim Replace As Object

Doc = ThisComponent

Replace = Doc.createReplaceDescriptor
For I = 0 To 2
  Replace.SearchString = changeFrom(I) //Error is here
  Replace.ReplaceString = changeTo(I)
  Doc.replaceAll(Replace)
Next I
End Function


REM  *****  BASIC  *****
Sub main
Dim changeFrom(3) As String
Dim changeTo(3) As String


changeFrom() = Array("<word2>", "<word3>", "<word1>")
changeTo() = Array("value1", "value2", "value3")
test( changeFrom , changeTo)
End Sub

一般 -

有没有人知道一个更好的方式让我做这个除了“基本”,这真的让我疯狂.. 我知道它可以用Python完成,但我希望有一个更简单的方法,问题是word文档有表格和需要定义的东西,所以我不能只是将模板复制/粘贴到一个java类并修改它..

谢谢!

2 个答案:

答案 0 :(得分:1)

基本函数ARRAY()返回包含数组的VARIANT。因此,如果您使用ARRAY功能,则必须使用Variants:

拨号
Function test ( ByVal changeFrom As Variant ,ByVal changeTo As Variant  )
Dim I As Long
Dim Doc As Object
Dim Replace As Object

Doc = ThisComponent

Replace = Doc.createReplaceDescriptor
For I = lbound(changefrom) To ubound(changefrom)
  Replace.SearchString = changeFrom(I) 'Error is here
  Replace.ReplaceString = changeTo(I)
  Doc.replaceAll(Replace)
Next I
End Function


REM  *****  BASIC  *****
Sub main
Dim changeFrom As variant
Dim changeTo As variant

changeFrom = Array("<word2>", "<word3>", "<word1>")
changeTo = Array("value1", "value2", "value3")
test( changeFrom , changeTo)
End Sub

或者,如果您不使用ARRAY函数,则可以使用String()数组:

Function test1 ( ByVal changeFrom() As String ,ByVal changeTo() As String  )
Dim I As Long
Dim Doc As Object
Dim Replace As Object

Doc = ThisComponent

Replace = Doc.createReplaceDescriptor
For I = lbound(changefrom) To ubound(changefrom)
  Replace.SearchString = changeFrom(I)
  Replace.ReplaceString = changeTo(I)
  Doc.replaceAll(Replace)
Next I
End Function


REM  *****  BASIC  *****
Sub main1
Dim changeFrom(2) As String
Dim changeTo(2) As String

changeFrom(0) = "<word2>"
changeFrom(1) = "<word3>"
changeFrom(2) = "<word1>"

changeTo(0) = "value1"
changeTo(1) = "value2"
changeTo(2) = "value3"

test1( changeFrom , changeTo)
End Sub

问候

阿克塞尔

答案 1 :(得分:1)

我认为数组总是作为引用传递,即使你声明参数是按值的。我需要验证这仍然是正确的,但称之为90%肯定。

提供的答案正确使用LBound和UBound仅使用为传递的数组设置的限制。这可能是您问题的最大部分。现在,说,你可以简单地宏,并做这样的事情:

Function test1 (Doc, changeFrom, changeTo)
Dim I As Long
Dim Replace As Object

' A paranoid person would verify the parameters. Things like:
' Are the parameters NOT IsEmpty and Not IsNull
' Is the Doc object really a text document
' Are the other parameters really arrays

Replace = Doc.createReplaceDescriptor
For I = lbound(changefrom) To ubound(changefrom)
  Replace.SearchString = changeFrom(I)
  Replace.ReplaceString = changeTo(I)
  Doc.replaceAll(Replace)
Next I
End Function


REM  *****  BASIC  *****
Sub main1
  test1(ThisComponent, Array("<word2>", "<word3>", "<word1>"), Array("value1", "value2", "value3"))
End Sub