Path of current document in [Libre|Open]Office

13,219

Solution 1

In the meantime I have found a feasible solution. Look here:

Sub RunSomeMovie
    GlobalScope.BasicLibraries.loadLibrary("Tools")
    Shell("/usr/bin/mplayer -fs ", 2, "" & Tools.Strings.DirectoryNameoutofPath(ThisComponent.getURL(),"/") & "/media/somemovie.mp4")
End Sub

Solution 2

to get the current document path you use this simple command:

ThisComponent.getURL()

CurDir() is simply not reliable for all OS BTW I found that info in the Pitonyak book you cited that I didn't knew and found very precious so thanks for sharing it.

Solution 3

I'm working with LibreOffice 4.2.0.4 and the following Code works for me:

    Dim Dir as String
    GlobalScope.BasicLibraries.loadLibrary("Tools")
    Dir = Tools.Strings.DirectoryNameoutofPath(ThisComponent.url, "/")

The variable "dir" contains now the path of the current document.

Solution 4

Document has a location only if it is saved (or opened from a saved). In addition a Form opened from within LibreOffice Base does not have by its own a location. Only the odt has a location. Hence although:

ThisComponent.hasLocation()

returns True, the location is an empty string (consider it as a bug). To obtain the location, in such cases, you have to access the "Parent":

ThisComponent.Parent.hasLocation() (or ThisComponent.Parent.getURL())

Of course you then have to "traslate" it to readable with: Tools.Strings.DirectoryNameoutofPath

Share:
13,219
dubbaluga
Author by

dubbaluga

Updated on June 06, 2022

Comments

  • dubbaluga
    dubbaluga almost 2 years

    How can I determine the path of the currently open document in an OpenOffice or LibreOffice document? I want to forward the path to an external application.

    More specifically I want to launch an external player from impress with a video file in full-screen mode:

    Shell("/usr/bin/mplayer", 2, """ -fs"" some-file-in-same-dir-as-document.mp4")
    

    For the last parameter I would need the path to the currently opened document in order to append it. Not specifying the path results in referring to the current path (as given by the CurDir() function) which is something different.

    Is there actually a good reference for LibreOffice Macros? The only relatively good (but hard to search) document I have found is "OpenOffice Macros Explained" by Andrew Pitonyak.

  • dubbaluga
    dubbaluga over 11 years
    Thank you! I can confirm that this is the answer I was looking for. To be honest I have to admit that I have found a solution in the meantime (please see below).
  • Antoni
    Antoni almost 2 years
    I use ubuntu and start a document from symlink. ThisComponent.getURL() returns symlink' path instead of location of the document. Is this a bug? LibreOffice 6.4.7.2.