Is there a way to get the current sheet name WITHOUT using "ActiveSheet"?

I want to get the sheet name On Print event. I don´t want to use “ActiveSheet” because I always get an error when I CANCEL the print job (error 423 - ActiveSheet). How do I get the sheet name WITHOUT, WITHOUT using “ActiveSheet” macro code?
Can I get sheet name by using "SupportsService(“com.sun.star.sheet.SpreadsheetDocument”) ?

I tried to use “oEvent.Source.getCurrentController().ActiveSheet”, but I keep on getting the “423 error-ActiveSheet” when I CANCEL the print job.
image

For me event.Source.CurrentController.ActiveSheet.Name returns the name as expected.
However, what do you want to use the info for in an onPrint routine?
Did you assign your Sub via >Tools>Customize?
What about the question concerning the supported service?
I would expect you bound the routine to the document. If this document isn’t a spreadsheet document, there’s no sense in aking for an active, sheet of course.
If the setting is stored to the local profile, on the other hand, you must test the document kind in advance of asking for a sheet.
Lupp’s confused.

The document print event procedure you specified is called 3 times each time the PrintableState changes.
Therefore, in your situation, you can use the On Error operator (which will avoid an error when canceling a job) or use XPrintJobListener (which allows you to get information about the PrintableState).

I tried to use " on error resume next" and “on error goto 0”, but I get the same error when I cancel the print job

Try:

Sub OnPrint(oEvent)
  On Error GoTo ErrLabel 
  Msgbox oEvent.Source.getCurrentController.ActiveSheet.Name
ErrLabel:  
End Sub

Do you have a example Of a XPrintjoblistener to get sheet name?

If the goal is to determine the name of the sheet to be printed, then the previous example should help.

No matter where I write ActiveSheet. The on print event doesn’t recognize It as a valid command. I always get the same error by canceling the print job…even “on error” procedure.

Your procedure will be called three times. On the first (JOB_STARTED ) and / or second (JOB_COMPLETED) call, you get the name of the sheet. On the third call when the job is canceled (JOB_ABORTED), it is no longer necessary to determine the sheet name; for this, the On Error statement was applied.
The situation with the Listener is similar.

I like to learn…do you have a example Of listener? Just to know It …I would appreciate that.

Call StartPrintJobListener.

Option Explicit
Global oPrintJobListener

Sub StartPrintJobListener
 oPrintJobListener=CreateUnoListener("PrintJob_","com.sun.star.view.XPrintJobListener")
 ThisComponent.addPrintJobListener oPrintJobListener
End Sub

Sub PrintJob_printJobEvent(oEvent)
  If oEvent.State=com.sun.star.view.PrintableState.JOB_STARTED Then
    Msgbox "Print sheet " & oEvent.Source.Printable.getCurrentController.ActiveSheet.Name 
  End if 
End Sub

Sub PrintJob_disposing(oEvent)
End Sub

Thanks a Lot.

I recommend taking a close look at the oEvent object using MRI. There are many interesting things there. :slightly_smiling_face:

See Andrew Pitonyak’s “Useful Macro Information”
14.3.5. Example 2: com.sun.star.view.XPrintJobListener
And of course “The Book” by the same author.
Yo can get (the pff, e.g) here: www.pitonyak.org/oo.php .

Unfortunately I keep on getting error…I don’t like using “Error Event”…the macro program gets like dirty.
I had to place the active sheet name in an annotation of cell A1 in third sheet (the A column of Third sheet is hidden). Every “Activate Document” event sheet changes the annotation in cell A1 of third sheet. On Print event, the macro code gets the string in cell A1 annotation of third sheet. For now, it´s the solution.

First you learn programming.
Then you master complex APIs.
UNO is a very complicated API, even for experienced programmers.

Please, try to formulate your tasks in as much detail as possible.
I can only (with difficulty) guess that you are printing a sheet from the Print Preview mode (there is no mention of this in the topic). Indeed, in this mode the Document Controller does not have getActiveSheet method. In this case, you can remember the active sheet with each change (as you did), or go a more complicated way (for example, through the use of AccessibleContext).

I would never have thought of that.

Indeed…
I do the program for my work coleagues to use it. I have to predict every possible actions that my coleagues can do on program…If a work coleague try to use the Print Preview, He will get an error. None of my colleagues knows programming. They barely know how to open the Libreoffice menu.
I tried to use the formula =SHEET() to get the index sheet by writing
oFunction = createUnoService(“com.sun.star.sheet.FunctionAccess”)
sMyString = oFunction.callFunction(“sheet”,array())
but I get the same number (2) every time…It’s weird.