[Solved] How to enumberate documents?

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
grnhorn
Posts: 32
Joined: Sun Jul 13, 2008 2:07 pm

[Solved] How to enumberate documents?

Post by grnhorn »

How do I check for all open soffice documents? I found out that if I use ThisComponent.Close(True) or ThisComponent.Dispose it leaves soffice.exe and soffice.bin processes running. If I use StarDesktop.Terminate(), it will shut down all processes, but it will also close all open documents.

If I use File>Exit or the close 'X' on the caption bar the processes shut down (this has got to be a bug). I will be more then glad to skip the previous question if anyone has an answer to this issue.

Thanks...
Last edited by grnhorn on Sun Aug 24, 2008 6:59 pm, edited 1 time in total.
OOo 2.4.X on Ms Windows XP
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How to enumberate documents?

Post by Villeroy »

Code: Select all

REM Basic
oEnum = StarDesktop.getComponents()
returns an enumeration of components (aka document models).
http://api.openoffice.org/docs/common/r ... Components
StarDesktop is a global helper variable in StarBasic similar to ThisComponent.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
grnhorn
Posts: 32
Joined: Sun Jul 13, 2008 2:07 pm

[Solved] How to enumberate documents?

Post by grnhorn »

Thanks Villeroy, here is the final code I came up with. The only issue I found is that BasicIDE is included as a document.

Code: Select all

Sub TerminateProcesses()
Dim sDocURL As String
Dim oDocs As Object
Dim oDoc As Object
Dim oComponents As Object

' Close document
    GlobalScope.BasicLibraries.LoadLibrary("Tools")
    sDocURL = ThisComponent.getURL()
    oDocs = StarDesktop.getComponents().createEnumeration()
    Do While oDocs.hasMoreElements()
		oDoc = oDocs.nextElement()
REM This line looks at the document you are working with and will terminate processes if nothing else is running;
REM otherwise it will close and leave the other running. 'Reverse "/" to get path without filename.
		If oDoc.URL <> DirectoryNameoutofPath(sDocURL, "\") Then ThisComponent.Close(True)
REM This line can be used to determine how many documents are open.
		'i = i + 1 'Counter
	Loop
REM This shuts down all running documents and terminates all running soffice processes
	StarDesktop.Terminate()	
End Sub
OOo 2.4.X on Ms Windows XP
Post Reply