Macro Programming: How to determine the calling application

I have created a macro which runs on start up to load the most recent document which works OK.

However, if my last document was a Calc sheet, when clicking on Writer it will open the Calc sheet in Calc.

So my question is: how do I determine the calling application in my OnStart App call? Knowing this I can filter the recent documents list for the correct type.

Alternatively, is there a way to set different OnStartApp macros for different applications?

The application is always libreoffice. It is one program for all file types. OpenOffice.org Macros Explained

I think that if you show the text of the macro, then together we can clarify.

It’s something I picked up (in this forum, I think):

Sub set_Start_Application_Event_To_Open_Most_Recent_Document()
REM Programmatically connect the "Start Application" event to the Basic method called: Open_Most_Recent_Document().
REM NB.
REM Make sure that the Basic method called "Open_Most_Recent_Document" exists inside [My Macros & Dialogs],
REM inside the library called "Standard", inside the module called "LibreOffice".
REM Then call this method once to connect the event.
REM NB. 
REM This causes the Most Recently opened Document to be opened *EVERY TIME* that the LO application starts up.

	set_Application_Event( "OnStartApp", "Standard", "LibreOffice", "Open_Most_Recent_Document" ) 
End Sub


Sub set_Application_Event( sEventName as String, sLibrary As String, sModule As String, sFunction As String )
REM Connects the specified Application Event to the specified Basic macro.
REM <sEventName>: The Name of the Application Event to connect a macro to.
REM <sLibrary>	: The Name of the Library that contains the module <sModule>.
REM <sModule>	: The Name of the Module that contains the method <sFunction>.
REM <sFunction>	: The Name of the Method to be connected to the specified Application Event.

	Dim aProps(1) As New com.sun.star.beans.PropertyValue
	aProps(0).Name		= "EventType"
	aProps(0).Value		= "Script"
	aProps(1).Name		= "Script"
	aProps(1).Value		= "vnd.sun.star.script:" & sLibrary & "." & sModule & "." & sFunction & "?language=Basic&location=application"
	
	Dim oGlobalEventBroadcaster As Object
	oGlobalEventBroadcaster = GetDefaultContext().getByName( "/singletons/com.sun.star.frame.theGlobalEventBroadcaster" )
	oGlobalEventBroadcaster.Events.replaceByName( sEventName, aProps() )
	msgbox sEventName & " connected to: " & oGlobalEventBroadcaster.Events.getByName( sEventName )(1).Value, 64, "Set Application Event"
End Sub


Function Open_Most_Recent_Document()
REM Opens the Most Recently opened LibreOffice Document, and returns it if succesful.

	On Local Error Resume Next
	fPath = getMostRecentDocument()
	sURL = ConvertToURL(fPath)
	Open_Most_Recent_Document = StarDesktop.loadComponentFromURL( sURL, "_blank", 0, Array() )
End Function


Function getMostRecentDocument() As String
REM Returns the URL of the most recently opened document in LibreOffice.
REM Returns an empty string if there are no recent documents.
	Dim aRecentDocs() As String
	aRecentDocs = getRecentDocuments(True)
	If uBound( aRecentDocs ) >= 0 Then getMostRecentDocument = aRecentDocs( 0 )
End Function


Function getRecentDocuments( Optional bOrdered As Boolean ) As Variant
REM	Returns an Array containing the URLs of LibreOffice Most Recent Documents.
REM <bOrdered>	: Pass <True> ( or leave empty ) to get the array sorted by most recent date;
REM				  Pass <False> to get the unordered List of Recent Documents.
	Dim aProps(1) As New com.sun.star.beans.PropertyValue
	aProps(0).Name	= "nodepath"
	aProps(0).Value	= "/org.openoffice.Office.Histories/Histories/"
	aProps(1).Name	= "enableasync"
	aProps(1).Value	= False
	
	Dim oConfig As Object, oHistory As Object
	oConfig		= createUnoService( "com.sun.star.configuration.ConfigurationProvider" )
	oHistory	= oConfig.createInstanceWithArguments( "com.sun.star.configuration.ConfigurationAccess", aProps() )

	If IsMissing( bOrdered ) Or bOrdered Then		  REM Create ordered item list:
	
		Dim oOrderList		As Object	:	oOrderList	= oHistory.PickList.OrderList
		Dim sOrderNames()	As String	:	sOrderNames	= oOrderList.getElementNames()
		Dim iMaxIndex		As Integer	:	iMaxIndex	= uBound( sOrderNames )
		Dim aentriesOrderedList( iMaxIndex ) As String
		Dim i As Integer
		
		For i = 0 To iMaxIndex
			aOrderedList( i ) = oOrderList.getByName( sOrderNames(i) ).HistoryItemRef
		Next
		
		getRecentDocuments = aOrderedList()
	Else
	
		getRecentDocuments = oHistory.PickList.ItemList.getElementNames()
	End If
End Function

(edited by ajlittoz for better readability)

ThisComponent.ImplementationName gives a clue about the type of the currently active documemnt

Yes, thank you, I can see how I could use that. :slightly_smiling_face:

I’m afraid that at the time of the OnStartApp event, this object has not yet been built.

This is not an answer but an additional question. Please move it either into original post or into a comment, then delete this “non-answer”. Thanks for abiding by the site rules.

Simply use some other event(s )such as “Open Document” and/or “New Document”. Or use no events at all. Working with templates, styles and using the customization features can save thousands of lines of stupid Basic code.