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

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.