I have some some XLSX spreadsheets that need to run on both Excel and LibreOffice.
They have numerous macros included.
Due to the differences between the platforms, I wanted to be able to tweak my macros to behave differently on the different platforms (so that they would then work without change and seamlessly for the end-users).
I was searching for a solution and found this topic : https://ask.libreoffice.org/t/detect-if-vba-is-running-under-libreoffice-or-excel/3841 but the “solution” proposed did not work because the LibreOffice Basic code would not compile under Excel.
I don’t have enough points to add a solution (or even a comment) to that closed topic, so here is a solution that works UNCHANGED on Excel, LibreOffice, and even OpenOffice.
Function whichSpreadsheetProgram() as String
svc = createUnoService( "com.sun.star.sheet.FunctionAccess" )
relString = svc.callFunction( "INFO", Array("Release"))
IF LEN(relString) > 6 then
IF Instr(relString,"Build") > 0 then
whichSpreadsheetProgram = "OPEN"
else
whichSpreadsheetProgram = "LIBRE"
end if
else
whichSpreadsheetProgram = "EXCEL"
end if
end Function
Obviously it could be adapted to return a number , or an abbreviated string (eg “LO”,“OO”,“XL”) if desired.
I hope this post helps someone else to save quite a few hours of trial and error
Cheers,
Warren