Page 1 of 1

[Solved] Accessing to a file other that ThisComponent

PostPosted: Sat Feb 18, 2017 10:06 pm
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")

Re: Accessing to a file other that ThisComponent

PostPosted: Sat Feb 18, 2017 11:36 pm
by FJCC
You can get a reference to the document by loading it like this
Code: Select all   Expand viewCollapse view
FileURL = convertToURL("c:\users\fjcc\desktop\FILE2.ods"
oDoc2 = StarDesktop.loadComponentFromURL(FileURL, "_blank", 0, Array())

Re: Accessing to a file other that ThisComponent

PostPosted: Sun Feb 19, 2017 2:41 am
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?

Re: Accessing to a file other that ThisComponent

PostPosted: Sun Feb 19, 2017 5:11 am
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   Expand viewCollapse view
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

Re: Accessing to a file other that ThisComponent

PostPosted: Mon Feb 20, 2017 3:06 am
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())

Re: Accessing to a file other that ThisComponent

PostPosted: Wed Feb 22, 2017 6:57 am
by Koosha
My Mistake.

The code worked perfect. Thank you.

Re: Accessing to a file other that ThisComponent

PostPosted: Wed Feb 22, 2017 7:00 am
by Koosha
Solved

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

PostPosted: Tue Nov 13, 2018 4:45 pm
by Villeroy
You could also replace the frame descriptor "_blank" (new frame) with "_default".
See http://www.openoffice.org/api/docs/comm ... entFromURL