1

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

首先 - Basic 是一场噩梦,通常整个宏编写过程也非常糟糕。

现在到“代码” - 我不断收到一些错误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类中并修改它..

谢谢!

4

2 回答 2

2

基本函数 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

问候

阿克塞尔

于 2014-08-07T14:50:16.263 回答
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
于 2015-10-29T13:02:54.200 回答