[Solved] Accessing to a file other that ThisComponent

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Koosha
Posts: 7
Joined: Sat Feb 18, 2017 9:56 pm

[Solved] Accessing to a file other that ThisComponent

Post by Koosha »

Hi
I need to access two worksheets in one macro. I need to transfer some data from file2.ods to current file (file1.ods).
Both of them are open and are in same directory.

document1 = ThisComponent
Sheets1 = document1.Sheets
Sheet1 = Sheets1.getByName("Destination")

"How to Define document2 ???"
Sheets2 = document2.Sheets
Sheet2 = Sheets2.getByName("Source")
Last edited by Hagar Delest on Wed Feb 22, 2017 5:26 pm, edited 2 times in total.
Reason: tagged [Solved].
OpenOffice 4, Windows 10
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Accessing to a file other that ThisComponent

Post by FJCC »

You can get a reference to the document by loading it like this

Code: Select all

FileURL = convertToURL("c:\users\fjcc\desktop\FILE2.ods"
oDoc2 = StarDesktop.loadComponentFromURL(FileURL, "_blank", 0, Array())
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Koosha
Posts: 7
Joined: Sat Feb 18, 2017 9:56 pm

Re: Accessing to a file other that ThisComponent

Post by Koosha »

Thank You. The solution you mentioned kind of worked, but the problem is every time I run the Marco, it opens the "file2.ods" in a new window; even if the file is already open! Is there a solution that recognize "open" files and don't re-open it?
OpenOffice 4, Windows 10
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Accessing to a file other that ThisComponent

Post by FJCC »

You can iterate over the open files and select the one you want. If you don't find it, you can load it. That would look something like

Code: Select all

Flag = 0
FileURL = convertToURL("c:\users\fjcc\desktop\FILE2.ods"
Components = StarDesktop.getComponents()
CompEnum = Components.createEnumeration
While CompEnum.hasMoreElements
	OneComp = CompEnum.nextElement
	If OneComp.URL = FileURL Then
		oDoc2 = OneComp
		Flag = 1
	End If
Wend
If Flag = 0 Then
	oDoc2 = StarDesktop.loadComponentFromURL(FileURL, "_blank", 0, Array())
End If
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Koosha
Posts: 7
Joined: Sat Feb 18, 2017 9:56 pm

Re: Accessing to a file other that ThisComponent

Post by Koosha »

I debuged the program and it seems it detects the open file and the flag becomes 1, however the file and sheets are not accessible:
MySheet = oDoc2.Sheets.GetByName("Sheet1")

The mentioned command was accessible when I opened the file by:
oDoc2 = StarDesktop.loadComponentFromURL(FileURL, "_blank", 0, Array())
OpenOffice 4, Windows 10
Koosha
Posts: 7
Joined: Sat Feb 18, 2017 9:56 pm

Re: Accessing to a file other that ThisComponent

Post by Koosha »

My Mistake.

The code worked perfect. Thank you.
OpenOffice 4, Windows 10
Koosha
Posts: 7
Joined: Sat Feb 18, 2017 9:56 pm

Re: Accessing to a file other that ThisComponent

Post by Koosha »

Solved
OpenOffice 4, Windows 10
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved] Accessing to a file other that ThisComponent

Post by Villeroy »

You could also replace the frame descriptor "_blank" (new frame) with "_default".
See http://www.openoffice.org/api/docs/comm ... entFromURL
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
Post Reply