Page 1 of 1

[Solved] Close file 2 open in the background

Posted: Mon Dec 31, 2018 6:46 am
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 .

Re: Close file 2 open in the background

Posted: Mon Dec 31, 2018 9:38 am
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.

Re: Close file 2 open in the background

Posted: Mon Dec 31, 2018 10:38 am
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

Re: Close file 2 open in the background

Posted: Mon Dec 31, 2018 5:57 pm
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 .

Re: Close file 2 open in the background

Posted: Mon Dec 31, 2018 6:13 pm
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?

Re: Close file 2 open in the background

Posted: Mon Dec 31, 2018 6:21 pm
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

Re: Close file 2 open in the background

Posted: Mon Dec 31, 2018 6:30 pm
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 .

Re: Close file 2 open in the background

Posted: Mon Dec 31, 2018 6:45 pm
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...)

Re: Close file 2 open in the background

Posted: Mon Dec 31, 2018 7:00 pm
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

Re: Close file 2 open in the background

Posted: Mon Dec 31, 2018 8:52 pm
by Math
[Solved]

thank you friends . :super:

hugs .