[Solved] Close file 2 open in the background

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Math
Posts: 89
Joined: Mon Oct 29, 2018 6:32 pm

[Solved] Close file 2 open in the background

Post by Math »

greetings ,

          I need to create a macro to close an external file, ie I am inside file 1 and want to press a button to close file 2 that is open in the background .

          How can I close an external file 2 when I'm positioned in file 1 ?

          in excel's vba is done as follows: sub userform_Terminate

          so what's the macro to do this operation in LibreOffice ?


hugs .
Last edited by Math on Mon Dec 31, 2018 8:53 pm, edited 1 time in total.
LibreOffice 5.4.4.2 on Windows 7
User avatar
Zizi64
Volunteer
Posts: 11352
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Close file 2 open in the background

Post by Zizi64 »

In your linked Excel macro sample code you MUST know the filename/URL of the another documents:

Code: Select all

Private Sub UserForm_Terminate()

    Windows("Pasta2.xlsm").Activate
    ActiveWindow.Close

End Sub


Here is a topic and a Sébastien C's sample code: how to get all of open documents, and how you can check the type (Spreadsheet, Text, etc...) of the documents.
viewtopic.php?f=20&t=93729#p445918
and an another sample:
viewtopic.php?f=9&t=79643#p366371

The "closing" methods:
oDoc1.close(True)
or
oDoc1.dispose()
http://www.openoffice.org/api/docs/comm ... html#close


And you must check (somehow) if the examined document is in the focus or not - before you close it by your macro.
Last edited by Zizi64 on Mon Dec 31, 2018 10:53 am, edited 2 times in total.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
Posts: 11352
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Close file 2 open in the background

Post by Zizi64 »

This macro will dispose (close) all of the open spreadsheet documents, except one where the macro was launched from.

Code: Select all

REM  *****  BASIC  *****

Sub Close_nonfocused_spreadsheet_documents
 Dim oCollection As Object
 Dim oDocWhereTheMacroWasLaunched
 Dim oNextDocument As Object
 Dim oFocus As Object

	oDocWhereTheMacroWasLaunched = Thiscomponent
	
	oCollection = starDesktop.getComponents().createEnumeration()

	Do While oCollection.hasMoreElements()
		oNextDocument = oCollection.nextElement()
		If oNextDocument.supportsService("com.sun.star.sheet.SpreadsheetDocument") Then
			If oNextDocument.location <> oDocWhereTheMacroWasLaunched.location then
				oNextDocument.Dispose()
			End if						
		End If
	Loop

End Sub
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Math
Posts: 89
Joined: Mon Oct 29, 2018 6:32 pm

Re: Close file 2 open in the background

Post by Math »

sr. Zizi64 ,

             the sub macro Close_nonfocused_spreadsheet_documents is very good for closing all open documents in the background .

             but there are moments that I have with the 2.ods file, 3.ods file, 4.ods file, 5.ods file ... open in the background, and then I want to close for example only the file 3.ods .

so I'm looking for the Macro to Close the File chosen.ods that is open in the background .

hugs .
Last edited by Math on Mon Dec 31, 2018 6:19 pm, edited 5 times in total.
LibreOffice 5.4.4.2 on Windows 7
User avatar
Zizi64
Volunteer
Posts: 11352
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Close file 2 open in the background

Post by Zizi64 »

but there are moments that I have with the 2.ods file, 3.ods file, 4.ods file, 5.ods file ... open in the background, and then I want to close for example only the file 3.ods .
If you know the string of the the actual
- URL
- file name and extension
then you can examine that propety of the open documents by API functions. (Is it possible if two document are open with same file name (but different URL)?

Have you installed at least one object inspection tool now? Check the properties of the objects you got by the programming loop.

Did you studied the API functions?
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Close file 2 open in the background

Post by RoryOF »

Depending on the method by which the file was opened, it may have a unique file number. Using this should allow it be closed independently of all other files.

File handing in macros is discussed in Pitonyak's OpenOffice.org Macros Explained, Chapter 8
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
Math
Posts: 89
Joined: Mon Oct 29, 2018 6:32 pm

Re: Close file 2 open in the background

Post by Math »

sr. RoryOF ,

I'm opening the files with the macro open a file called , do sr. FJCC .

so how can I close the file chosen.ods ?


hugs .
LibreOffice 5.4.4.2 on Windows 7
User avatar
Zizi64
Volunteer
Posts: 11352
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Close file 2 open in the background

Post by Zizi64 »

If you open the document with one subroutine, but you want to close it with an another subroutine, then you need store the document object in a global scope variable. Then you will able to close it by reference to the variable name.
https://wiki.openoffice.org/wiki/Docume ... _Variables

Code: Select all

 Dim Global oDoc1 as object 

Code: Select all

Sub_1
'...
oDoc1 = StarDesktop.loadComponentFromURL(FileURL, "_blank", 0, Array())
'...
End sub

Code: Select all

Sub_2
'...
oDoc1.dispose()
'...
End sub

In other cases you need identify the document to close by other properties (URL, filename.extension, etc...)
Last edited by Zizi64 on Mon Dec 31, 2018 8:55 pm, edited 2 times in total.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Close file 2 open in the background

Post by RoryOF »

That code is almost identical to that given in Pitonyak 12.4.2 and opens the file as a template; as far as I know, it is not given a name, so is not identifiable by a name.

A macro for closing a document is given in Pitonyak 13.8, listing 286. One may be able to use the dispose() method, but have regard to what Pitonyak says in 13.8.

Document handling is dealt with in Pitonyak 8, 12.4, & 13
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
Math
Posts: 89
Joined: Mon Oct 29, 2018 6:32 pm

Re: Close file 2 open in the background

Post by Math »

[Solved]

thank you friends . :super:

hugs .
LibreOffice 5.4.4.2 on Windows 7
Post Reply