[Solved] Form button to open url saved in a field?

Creating and using forms
Post Reply
giooo
Posts: 6
Joined: Wed Mar 03, 2010 2:41 pm

[Solved] Form button to open url saved in a field?

Post by giooo »

Hi everybody,

Say I have a table with a field containing web addresses.

In a form I'd like to add a button to open the browser to the web address stored in that record.
I thought of using a button with "open document/web page", but in that case the link is static.
See attached file for example. There's one table and one form.

Is it possible to pass somehow a field value to a button?
Any other strategies?

Maybe it's a trivial problem and I am missing something...
Thanks for any suggestions/help!

giooo
Attachments
db_link.odb
(11.33 KiB) Downloaded 901 times
Last edited by giooo on Fri Mar 05, 2010 10:19 am, edited 1 time in total.
OpenOffice 3.2 32-bit on Windows 7 Professional 64-bit
RPG
Volunteer
Posts: 2250
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Form button to open url saved in a field?

Post by RPG »

Hello

This routine must do what you asked You have to adjust some names.
For information how to connect a macro to an event see your help file

select first the help files.
then in the list box left in top : OpenOffice.org Base
tabpage index and type in : events;assigning scripts

Romke

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. ' Name before OOo3.2
' The name is new in OOo3.2 and later : Approve action
' The function can be aborted when the files does not exist.
' You have also change the properties of the button control.
' The action is :Open document/webpage.
' The small macro move the field value from the textbox to the button control url
function openurl(oEv as object)
' 
	dim oForm,oTextBox
	oForm=oEv.source.model.parent
	oTextBox=oForm.getbyname("TextBox")
	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
	'print  convertfromurl(oTextBox.text)
End function
 Edit: I have edit some bugs. 
Last edited by RPG on Sat Jul 11, 2015 10:05 am, edited 1 time in total.
LibreOffice 7.1.4.2 on openSUSE Leap 15.2
giooo
Posts: 6
Joined: Wed Mar 03, 2010 2:41 pm

Re: Form button to open url saved in a field?

Post by giooo »

yup, it works! :-)

thank you very much!


giooo
OpenOffice 3.2 32-bit on Windows 7 Professional 64-bit
tx42
Posts: 3
Joined: Thu Oct 04, 2012 10:56 pm

Re: [Solved] Form button to open url saved in a field?

Post by tx42 »

For opening a web URL, "FileExists" will usually hang and may crash OO!

:alarm: I abandoned opening web URLs with the macro above because it always hanged. I've since brushed up on my OO basic, and found the problem. It makes no sense to check if the file exists if the file is a web URL. Just check if its an empty string and let the browser do any further checking, etc. Here is a better version for opening web URLs based on the above code:

Code: Select all

REM **********************************************************************
REM Open URL from button
REM       https://forum.openoffice.org/en/forum/viewtopic.php?f=39&t=28228
REM **********************************************************************
' 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 commencing. ' Name before OOo3.2
' The name is new in OOo3.2 and later : Approve action
' The function can be aborted when the files does not exist.
' You have also change the properties of the button control.
' The action is :Open document/webpage.
' The small macro move the field value from the textbox to the button control url
sub openURL(oEv as object, aTextName as String)
   dim oForm, oTextBox
   oForm=oEv.source.model.parent
   oTextBox=oForm.getbyname(aTextName)
   if (oTextBox.text <> "") then
      oEv.source.model.targeturl = oTextBox.Text
      openurl=true ' Do the programm when the file exists
    else
      openurl=false ' The file does not exist and do nothing
   end if
   'print  convertfromurl(oTextBox.text)
End sub

sub openURLtxtURL(oEV as object)
   openurl(oEv, "txtURL")
End sub
With these two subroutines, you can reuse the openURL subroutine for text boxes of different names by defining different stubs. The above stub called "openURLtxtURL" opens the URL in the text box called "txtURL".
NeoOffice 3.2 on OS X 10.6
Koa
Posts: 12
Joined: Sun Jan 15, 2017 11:53 pm

Re: [Solved] Form button to open url saved in a field?

Post by Koa »

Thank you! I had some trouble figuring out what you meant by, "The action is :Open document/webpage.", but finally realized that in Control Properties General there was an Action property that I forgot about.
LibreOffice 5.2.5.1 on Linux Jessie 8.6
johnh009
Posts: 7
Joined: Thu Mar 16, 2017 2:00 pm

Re: [Solved] Form button to open url saved in a field?

Post by johnh009 »

I appreciate that this post is 'Solved', but I have a question that is linked that hopefully someone could advise me upon.

While the solution provided works for the original problem (open an URL), I would like the same functionality to work for a file (the file path being passed to the Push Button). I've tried with the given basic code (macro), but get the following message:

Code: Select all

"/home/path/name" is not an absolute URL that can be passed to an external application to open it.
Must be some specific code within the macro, because when the path to a file is typed into the URL property of the Push Button, the file is opened.
LibreOffice 5.2.6.2 (Build ID:1:5.2.6-1). Linux (Sparkylinux based on Debian) 4.8
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved] Form button to open url saved in a field?

Post by Villeroy »

Last weeks
johnh009 wrote:I appreciate that this post is 'Solved', but I have a question that is linked that hopefully someone could advise me upon.

While the solution provided works for the original problem (open an URL), I would like the same functionality to work for a file (the file path being passed to the Push Button). I've tried with the given basic code (macro), but get the following message:

Code: Select all

"/home/path/name" is not an absolute URL that can be passed to an external application to open it.
Must be some specific code within the macro, because when the path to a file is typed into the URL property of the Push Button, the file is opened.
Last week's topic: viewtopic.php?f=13&t=92140
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
johnh009
Posts: 7
Joined: Thu Mar 16, 2017 2:00 pm

Re: [Solved] Form button to open url saved in a field?

Post by johnh009 »

@Villeroy. Thank you for your prompt response. After following the link you provided and attempting to follow it, it all looked far more complex, with more functionality than I required. So I returned to the original code, had a little read of the Basic manual - the last resort - and discovered the following:

"Since OpenOffice.org is a platform-independent application, it uses URL notation (which is
independent of any operating system), as defined in the Internet Standard RFC 1738 for file
names. Standard file names using this system begin with the prefix file:/// followed by the
local path."

So amending one line of basic,concatenating "file://" to the retrieved pathname string, thus . . .

oEv.source.model.targeturl = "file://"+oTextBox.Text

. . . did the trick. Thanks again.

Having read a little more, the correct way is probably . . .

oEv.source.model.targeturl = ConvertToUrl(oTextBox.Text)
LibreOffice 5.2.6.2 (Build ID:1:5.2.6-1). Linux (Sparkylinux based on Debian) 4.8
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved] Form button to open url saved in a field?

Post by Villeroy »

Today I got an inspiration for a slightly more simple version of that macro. I wrapped it into another example database together with an installer (a macro that installs another macro). viewtopic.php?f=100&t=92331&p=438435#p438435
If you just have one or more columns of fully qualified system paths then you can simply install my new Python macro with one click, draw some button on your own form, change 2 button properties and assign a macro to the "Approve Action" event to "My Macros" > pyDBA > URLButton > FileButton_Approve (this variant includes the conversion from system path to file-URL).
You don't need to program anything at all. Just add the name of the column to the button's "additional info" property.
The new macro does not rely on naming conventions and it does not use form events. Just tell your button about the column name and which macro to call.
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