0

I have a macro for libreoffice that takes in 2 arguments.

  1. A file path
  2. Variable args that are sheet names to look for.

I iterate over the document pertaining to the first arg and get the sheet that matches the first variable arg. I then convert that sheet to CSV.



Sub ExportToCsv(URL as String, ParamArray sheetNames() As Variant)
  Dim saveParams(1) as New com.sun.star.beans.PropertyValue
  saveParams(0).Name = "FilterName"
  saveParams(0).Value = "Text - txt - csv (StarCalc)"
  saveParams(1).Name = "FilterOptions"
  saveParams(1).Value = "44,34,0,1,1" ' 44=comma, 34=double-quote

  GlobalScope.BasicLibraries.loadLibrary("Tools")
  URL = ConvertToURL(URL)
  document = StarDesktop.loadComponentFromUrl(URL, "_blank", 0,  Array())

  baseName = Tools.Strings.GetFileNameWithoutExtension(document.GetURL(), "/")
  directory = Tools.Strings.DirectoryNameoutofPath(document.GetURL(), "/")

  sheets = document.Sheets
  sheetCount = sheets.Count
  Dim x as Integer
  Dim requiredSheetIndex as Integer
  For x = 0 to sheetCount -1
    sheet = sheets.getByIndex(x)
    sheet.isVisible = True
    For i = LBound(sheetNames) To UBound(sheetNames)
      If StrComp(sheet.Name, sheetNames(i), vbTextCompare) = 0 Then
        requiredSheetIndex = x
      End If
    Next
  Next

  currentSheet = document.GetCurrentController.GetActiveSheet()
  sheet = sheets(requiredSheetIndex)
  document.GetCurrentController.SetActiveSheet(sheet)
  filename = directory + "/" + baseName + ".csv"
  fileURL = convertToURL(Filename)
  document.StoreToURL(fileURL, saveParams())
  document.close(True)
End Sub

Eg. ExportToCsv(<path>, 'Data'). Suppose the document has 4 sheets with the 3rd sheet as DATA, this sheet should be converted to CSV.

Previously I used to give the sheet idnex directly into the macro and it worked perfectly. But requirements changed and I have to pass in an array of possible names to match on. Hence the variable args.

But now I am getting a syntax error(Screenshot attached). I cant figure out what is wrong here.

Error screenshot

BigBen
  • 46,229
  • 7
  • 24
  • 40
leoOrion
  • 1,833
  • 2
  • 26
  • 52
  • Don't you just need `Sub ExportToCsv(URL as String, sheetNames As Variant)` ? Or because you're on `libreoffice` at least get rid of the brackets for `sheetNames()` (not sure what the `ParamArray` is for either). – jamheadart May 19 '20 at 12:25
  • The macros tag says "DO NOT USE for VBA", so removed. – BigBen May 19 '20 at 12:31
  • Ok. I was not aware. Thanks – leoOrion May 19 '20 at 12:31
  • @jamheadart I am not well versed with vba. I was using this as a reference - `https://stackoverflow.com/questions/14716385/variable-argument-list-with-visual-basic` – leoOrion May 19 '20 at 12:32

1 Answers1

3

I believe it is complaining about the ParamArray keyword. After a little digging, I discovered you need to include:

option compatible

at the top of your module. For more information, you can refer to this link.

Brian M Stafford
  • 8,483
  • 2
  • 16
  • 25
  • This worked. Thanks. I am still facing issues in other areas but the compilation has succeeded now. – leoOrion May 19 '20 at 13:32