OOBasic and Calc: "property or method not found" when printing a sheet

Hello,

I’m trying to create a BASIC macro for Calc where I have data in a sheet and, when I execute my macro, some of the data is copied into another sheet and that second sheet is then printed.

After searching on the web, my code for printing is the following: (I don’t include the data-copying part because this part works)

 '   Sheets("facture").PrintOut

 Dim oDoc as Object
oDoc=ThisComponent
Dim sPrefix$  ' Prefix used to identify the print listener routines.
Dim sService$ ' Print listener service name

sPrefix="print_listener_"
sService="com.sun.star.view.XPrintJobListener"
If NOT oDoc.sheets().hasByName("facture") Then
  MsgBox "Le document n'a pas de page nommée 'facture'"
Else
  Dim oSheets
  oSheets = oDoc.Sheets
  Dim currentSheet
  currentSheet = oDoc.getcurrentcontroller.activesheet
  
  oDoc.currentController.setActiveSheet(oSheets.getByName("facture"))
  oPrintListener=CreateUnoListener(sPrefix,sService)
  oDoc.addPrintJobListener(oPrintListener)
  ' oPrintJobListnerDoc=oDoc
  oDoc.Print(Array())
  
  wait 600

  oDoc.currentController.setActiveSheet(currentSheet)
End If

This code doesn’t work: it pops up plenty of windows with “BASIC runtime error, Property or method not found: $(ARG1)” and no further info.

If I comment out the “wait 600” and “oDoc.currentController.setActiveSheet(currentSheet)” lines, the code works with no error and the sheet is printed, but it doesn’t go back to the sheet where I started the macro from (that’s the behavior I want).

If I only comment out the “wait 600”, LibreOffice stays on the sheet from which I started the macro, doesn’t switch to the one I want to print (visually at least) and prints an empty page.

Can anybody help me? Thanks!