[Solved] Errors with open document via button in form

Creating and using forms
Post Reply
Doranwen
Posts: 83
Joined: Sun May 26, 2019 6:46 am

[Solved] Errors with open document via button in form

Post by Doranwen »

I had some excellent help earlier this year setting my database here, here, and here. For reference, I'm using LibreOffice 5.1.6.2 with a split database on Linux Mint.

So, I realized today that it was probably possible to set up something so that I could click a button in the form and open the file (each record refers to a specific file on my hard drive) in whatever program my computer could do it in. Sure enough, when I did a search, I found this thread detailing how to do that.

However, I must have made an error of some kind when following the directions, because when I put in the full file path (filename, extension, and all) and went forward and backward records to make sure it's been stored in the database, clicking on the button I assigned to open it only results in the following error:

Code: Select all

BASIC runtime error.
An exception occurred 
Type: com.sun.star.container.NoSuchElementException
Message: .
I'm pretty sure it can't find the element I'm trying to tell it to find, in the macro, so I probably made an error in what I replaced the example names with. The form I'm putting the button and all that in is titled MainForm and the text box that would contain the filename/path is txtOfflineLink. The actual table it's on is Fanfics, and the column is OfflineLink.

What I put is (copied from the other thread, with only substituting the names):

Code: Select all

REM  *****  BASIC  *****
' You need two controls in your form(document). They must be in the same form(as in navigator).
' One control is a text control and contains the pathname or url to the file.
' The second control is a button.
' The action is bound to the first event of the button control: Before commenting
' The name is new in OOo3.2 and later : Approve action
' The function can be aborted when the files does not exist.
' You also have change the properties of the button control.
' The action is :Open document/webpage.
' The small macro moves the field value from the textbox to the button control url
function openurl(oEv as object)
   dim oForm as object
   dim oTextBox as object
   dim oTextBoxtest as object
   dim oMRI as Object
   
oDoc = ThisComponent
   oForm=oEv.source.model.parent
   oTextBox=oForm.getbyname("MainForm").getbyname("txtOfflineLink")
      if FileExists ( oTextBox.text) then
        oEv.source.model.targeturl=oTextBox.text
        openurl=true ' Do the program when the file exists
      else
        openurl=false ' The file does not exist and do nothing
      end if
End function
I also tried it with the names from the actual table (Fanfics and OfflineLink) and got the same result. Anyone able to explain what I'm doing wrong and how to fix it? (In case it's helpful to know, they're just pdf files, generally - in rare occasions, there would be an epub instead, which I have FBReader installed for.)
Last edited by Hagar Delest on Tue Oct 29, 2019 8:55 am, edited 1 time in total.
Reason: tagged solved
LibreOffice 5.1.6.2 on Linux Mint 18.1
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Errors with open document via button in form

Post by Villeroy »

Use this: viewtopic.php?f=100&t=92331&p=438435#p438435
It works with
-- file paths C:\Path\Doc.pdf
-- mail addresses foo@test.org
and URLs of all kinds including file:///Path/Doc.pdf, mailto:foo@test.org and https://openoffice.org/
without the need to change any code. Just set up your button(s) properly.
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
Doranwen
Posts: 83
Joined: Sun May 26, 2019 6:46 am

Re: Errors with open document via button in form

Post by Doranwen »

That's actually far more complicated than I need - the only things that are going to be in that box are file paths to pdfs or epubs on a Linux system (but they're all in various folders!), never a hyperlink or email address, and I already have the button event code set up fine. I mean, I appreciate the help, but it's kind of overkill, and appears to be a good deal more work to figure out than to just figure out how to make my case work for me. The examples in your post seem to be for Windows, which is a different file path type - mine being /home/username/restofthefilepath for root - and it would be easiest to code into the macro itself to treat all of them as files. (I'm also wondering if the spaces in the file names are an issue at all, and if so, how to deal with that.)

I tried to follow your guidelines, but while it prompted me to save the python code, it never appeared anywhere as a usable macro that I could find. So I think it would be easier for someone to tell me what I did wrong in the simple code replacement than to try to implement a much more complicated setup for a very strict sort of use-case.
LibreOffice 5.1.6.2 on Linux Mint 18.1
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Errors with open document via button in form

Post by Villeroy »

I wrote that little macro suite to make things a lot easier. It is a one-click install. Then you enter the name of the URL column as the button's additional info assign one of the macros to the button's "Approve" event.
You find the macros in "My Macros"-->pyDBA-->URLButton in any event dialog. You do not find it in the Basic hierarchy of libraries and modules since it is not a Basic macro.
If your data field contains system paths starting like C:\Path\ or /home/myself/Documents then you use macro "FileButton_Approve". The macro will convert this into an URL string which can be opened.
If your data field contains full URLs starting like file:///C:/Path/ or file:///home/myself/Documents then you use macro "URLButton_Approve". This macro will take the given URL literally.

And this one works with many URL buttons on the same form. Just write the right field name to the respective push button's additional info.
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
Doranwen
Posts: 83
Joined: Sun May 26, 2019 6:46 am

Re: Errors with open document via button in form

Post by Doranwen »

It's not there, though - I've checked several times under My Macros, and it's definitely not there. I've opened the form and clicked Install script - I did it a second time just to be sure. It says it saved it successfully, but it doesn't appear where you said it would. So I'm just confused now.
LibreOffice 5.1.6.2 on Linux Mint 18.1
chrisb
Posts: 298
Joined: Mon Jun 07, 2010 4:16 pm

Re: Errors with open document via button in form

Post by chrisb »

hello Doranwen,
You Said:
The form I'm putting the button and all that in is titled MainForm
this means that the object represented by the variable 'oForm' is in fact 'MainForm'.
it's unlikely that you have a main-form called 'MainForm' & a sub-form called 'MainForm'.
i think you need to modify the code like so:

Code: Select all

Function openurl(oEv as object)
   dim oForm as object, oTextBox as object
   oForm=oEv.source.model.parent
   oTextBox=oForm.getbyname("txtOfflineLink")
   if FileExists(oTextBox.text) then
     oEv.source.model.targeturl=oTextBox.text
     openurl=true ' Do the program when the file exists
   else
     openurl=false ' The file does not exist and do nothing
   end if
End Function
open office 4.1.14 & LibreOffice 6.4.4.2 x64 using HSQL 1.8.0.10 (Embedded) and HSQL 2.6.0 (Split) on Windows 10
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Errors with open document via button in form

Post by Villeroy »

If you are using the pre-installed office which does not include the Python bridge:

Code: Select all

sudo apt install libreoffice-script-provider-python
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
Doranwen
Posts: 83
Joined: Sun May 26, 2019 6:46 am

Re: Errors with open document via button in form

Post by Doranwen »

Chris: That did indeed work! Thank you!!!!!!!!

One final question: I had to put the file:// in front of the document path name in order to make it work. Is there a way to modify the code to automatically insert that in front of every single one when I go to open? (since all of them are going to be files, not web urls or emails) It would be a lot simpler if I didn't have to type that in front of all of them, just put the file path and document name+extension.
LibreOffice 5.1.6.2 on Linux Mint 18.1
chrisb
Posts: 298
Joined: Mon Jun 07, 2010 4:16 pm

Re: Errors with open document via button in form

Post by chrisb »

i am not familiar with Linux.
i assume that you are you inserting 'file://' into your text box.
if so then i suggest that it forms part of the file path & should therefore be stored as such.

if you have a table called 'tFilePath' which contains a text field called 'Path' (where the URL's are stored) then:
1) backup your table.
2) in the code below replace 'tFilePath' with your table name.
3) in the code below replace 'Path' with your field name.
4) execute the code in 'menu:Tools>SQL'
5) hit the table icon in the main Base window then 'menu:View>Refresh Tables'

Code: Select all

update "tFilePath" set "Path" = 'file://' || "Path";
ALTERNATIVELY i think that you could probably alter the macro code like so:

Code: Select all

   if FileExists("file://" + oTextBox.text) then
     oEv.source.model.targeturl= "file://" + oTextBox.text
open office 4.1.14 & LibreOffice 6.4.4.2 x64 using HSQL 1.8.0.10 (Embedded) and HSQL 2.6.0 (Split) on Windows 10
Doranwen
Posts: 83
Joined: Sun May 26, 2019 6:46 am

Re: Errors with open document via button in form

Post by Doranwen »

Ah, yes, Linux handles file paths similar to Windows with two notable differences:

- instead of back slashes, it uses forward slashes
- instead of C:\, there's "root", which is just a forward slash /

So my file paths + document name all look like (obviously different folders but this is a generic idea):
/home/username/Documents/filename.pdf
or
/media/username/Documents/filename.pdf

Adding "file://" in front of the full file path + document name tells my OS that it's a file and needs to be handled by the OS's default file handling system.

SWEEEEEET!!!! I tested the alternate code - as I think I understand what you're doing with it now and it makes sense. (I've never had any instruction in coding other than a semester class in high school of which I remember only the basics of HTML from, but if I study code hard I often can figure out what's going on in it.) And it works! The files pop open instantly. :D I am so delighted, you have no idea. Thank you very much!
LibreOffice 5.1.6.2 on Linux Mint 18.1
Post Reply