Page 1 of 1

[Solved] How to enumberate documents?

PostPosted: Sun Aug 24, 2008 3:22 pm
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...

Re: How to enumberate documents?

PostPosted: Sun Aug 24, 2008 4:47 pm
by Villeroy
Code: Select all   Expand viewCollapse view
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.

[Solved] How to enumberate documents?

PostPosted: Sun Aug 24, 2008 6:59 pm
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   Expand viewCollapse view
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