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

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 .

Word has it that the GetActiveSheet function handles both normal view and print preview modes.

Jonjozz, Please uplad an ODF type sample file with the full code of your macro embedded.

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.

Let’s try another way. Should work in any mode (normal or Preview) .

Option Explicit

' Returns the active sheet of Calc document.
' oDoc - Calc document.
Function GetActiveSheet(ByVal oDoc) As Object
  Dim oController, v
  GetActiveSheet=Nothing
  oController=oDoc.CurrentController
  If HasUnoInterfaces(oController, "com.sun.star.sheet.XSpreadsheetView") Then
    GetActiveSheet=oController.ActiveSheet
  Else  ' PreView Mode
    For Each v In oDoc.ViewData.getByIndex(0)
      If v.Name="ActiveTable" Then
        GetActiveSheet=oDoc.Sheets.getByName(v.Value)
        Exit Function
      End If    
    Next v
  End If  
End Function
 
Sub Test
  Msgbox GetActiveSheet(ThisComponent).Name
End Sub
1 Like

It worked great…You must be a macro code master…I can get no information about many codes in the macro lines…
“HasUnoInterfaces”
“ThisComponent.ViewData.getByIndex(0)”
v.Name=“ActiveTable”
You have a very complex knowledge.
Awesome!!!
Thanks very very very much for your attention and concern.

HasUnoInterfaces Function.
The description for Viewdata is really not very eloquent. My knowledge is the result of research through the wonderful MRI program.

He looks it up like anybody else does.