Close another workbooks

how to make a macro to close one or more calc workbooks, other than the one on which this macro is running?

You can iterate all opened window and make a closing for example according to the window Title. Or use other property in oComponent (use mri oComponent or xray oComponent).

Sub closeCalcWindows
	dim oComponents as object, oComponent as object
	oComponents=StarDesktop.getComponents().createEnumeration()
	if (NOT isNull(oComponents)) then 'iterate all opened LibreOffice windows
		do while oComponents.hasMoreElements()
			oComponent=oComponents.nextElement() 'current window
			if oComponent.Identifier="com.sun.star.sheet.SpreadsheetDocument" then 'it is Calc
				if oComponent.Title<>"Untitled 1" then 'title of the file that stays opened
					oComponent.close(true) 'close the window
				end if
			end if
		loop
	end if
End Sub
1 Like

@KamilLanda: I was expecting unusual side effects when I remove elements from a container I am iterating over - but it seems to work!
I’m also test with similar python-code:

def close_all_except(name):
    desktop = XSCRIPTCONTEXT.getDesktop()
    docs = desktop.Components
    for doc in docs:
        if (doc.supportsService("com.sun.star.sheet.SpreadsheetDocument")
            and doc.Title != name):
            doc.close(True)

according to @sokol92:

def close_all_except( document ):
    desktop = XSCRIPTCONTEXT.getDesktop()
    docs = desktop.Components
    for doc in docs:
        if (doc.supportsService("com.sun.star.sheet.SpreadsheetDocument")
            and doc.RuntimeUID != document.RuntimeUID):
            doc.close(True)
1 Like

LO can have multiple open documents with the same Title. Better to use RuntimeUID.

2 Likes

… but different URLs, so +1 for RuntimeUID because any “Untitled…” has no URL

but if i have 2 document open , what i can do