Detect if VBA is running under LibreOffice or Excel (or OpenOffice)

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 :slight_smile:

Cheers,

Warren

In Excel, doesn’t that fail already on the createUnoService() call because there is no such in VBA?

Files in .xlsx format cannot contain macros.


One way to write “bilingual” macros is described here.

Sorry , you are right. I got a bit carried away with the recently (for me) discovered create.uno.service when I posted :pensive:

What I actually implemented in my working solution , was having a NAMED cell (workbook global scope) with formula =INFO(“Release”).

And then I checked that in the Function

Function whichSpreadsheetProgram() as String
	relString = Range("RELEASE_INFO").value 
	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

Sorry about the over-exuberance earlier :slight_smile:

Cheers