[Solved] Accessing to a file other that ThisComponent

Keyboard macros or custom scripts

[Solved] Accessing to a file other that ThisComponent

Postby Koosha » Sat Feb 18, 2017 10:06 pm

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
Koosha
 
Posts: 7
Joined: Sat Feb 18, 2017 9:56 pm

Re: Accessing to a file other that ThisComponent

Postby FJCC » Sat Feb 18, 2017 11:36 pm

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())
Windows 10 and Linux Mint, since 2017
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 7230
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Accessing to a file other that ThisComponent

Postby Koosha » Sun Feb 19, 2017 2:41 am

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
Koosha
 
Posts: 7
Joined: Sat Feb 18, 2017 9:56 pm

Re: Accessing to a file other that ThisComponent

Postby FJCC » Sun Feb 19, 2017 5:11 am

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
Windows 10 and Linux Mint, since 2017
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 7230
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Accessing to a file other that ThisComponent

Postby Koosha » Mon Feb 20, 2017 3:06 am

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

Postby Koosha » Wed Feb 22, 2017 6:57 am

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

Postby Koosha » Wed Feb 22, 2017 7:00 am

Solved
OpenOffice 4, Windows 10
Koosha
 
Posts: 7
Joined: Sat Feb 18, 2017 9:56 pm

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

Postby Villeroy » Tue Nov 13, 2018 4:45 pm

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, no OpenOffice, LibreOffice 6.x
User avatar
Villeroy
Volunteer
 
Posts: 26980
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany


Return to OpenOffice Basic, Python, BeanShell, JavaScript

Who is online

Users browsing this forum: Google [Bot] and 47 guests