Inside a function, the function name is the return value and behaves as a Local Variable …
Except sometimes it doesn’t. WTF?
(and I can’t see any way to attach the example code, so here it is):
REM ***** BASIC *****
Option Explicit
''' Using the Function Name as a Local Variable works ...
Function bValidate() as boolean
bValidate = ThisComponent.supportsService("com.sun.star.sheet.SpreadsheetDocument")
If NOT bValidate Then
MsgBox "This macro works only on a spreadsheet"
End If
End Function
Function bActivateSheet(ByVal sName as string) as boolean
bActivateSheet = bValidate()
''' Perfectly okay to use Function Name as Local Variable in 'If' statement
If bActivateSheet Then
ThisComponent.getCurrentController().setActiveSheet( _
ThisComponent.getSheets().getByName(sName))
End If
End Function
''' BUT ONLY SOMETIMES
''' And I didn't care to spend any more hours tracking down whether it is an
''' issue involving an object or whether it is a syntax error
Function oCopySheet(ByVal sName as string, ByVal sTemp as string)
dim oController as object, oSheets as object
oController = ThisComponent.getCurrentController()
oSheets = ThisComponent.getSheets()
oSheets.copyByName(sName, sTemp, 32767)
oCopySheet = oSheets.getByName(sTemp)
''' Stick a MsgBox right here and you can find out it is recursing
''' ... and guess what I was doing ...
''' One or more of these RECURSE rather than recognizing the Function Name as a Local Variable
''' and give the wonderfully useless runtime error message "Argument not optional"
''' Neither uses a function call SYNTAX
oController.setActiveSheet(oCopySheet)
oCopySheet.Unprotect("")
End Function
Sub TestActivate
If bActivate(ThisComponent.getCurrentController().getActiveSheet().getName()) Then
MsgBox "Redundantly Activated Active Sheet"
Else
MsgBox "This won't happen unless you are not on a spreadsheet"
End If
End Sub
Sub TestCopy
oCopySheet("Sheet1", "SheetCopy")
End Sub
Sorry, I forgot to give its name rank and serial number:
LibreOffice 7.5…2 50(Build:2)
about:
Version: 7.5.6.2 (X86_64)
Environment: 8 threads; Linux 5.18
…
Ubuntu package version: 4.7.5.6_rc2-0ubuntu0.20.04.1-lo1
Calc: threaded