In base how to open a URL field?

If a text field holds a URL string, how can one easily open the link in your browser?

(A similar question is here: Hyperlink field types in Base)

If you want to be able to select the URL and hit F9 to open it in your browser, then just install this macro for the control’s KeyPressed event:

' --- If F9 key hit then execute the URL (i.e. show the file or web page) -------------'
Sub KeyPressedEvent(oEvent As Object)
  If oEvent.KeyCode =  com.sun.star.awt.Key.F9 Then   'Adjust to suit your taste'
    		
    '-Create the needed SystemShellExecute service object'
    DIM oShell AS OBJECT :oShell=createUnoService("com.sun.star.system.SystemShellExecute")
    	
    '-Get the URL from the current field'
    DIM s AS STRING      :s     =oEvent.Source.Model.CurrentValue : s = convertToUrl(s)

    '-Open the URL in your browser'
    oShell.execute(s,,0)
  End If
End Sub

Tip: So that when a user hovers over the text box field they get a tool tip, please also set the Help text property on the Control to something like:

Help text:   Hit F9 to open URL in your browser or other appropriate tool.

Updated Oct, 2018 to simplify and improve code.

Hello,

I find using a button to be quite effective. With this macro:

Sub GetURL
	oForm = ThisComponent.Drawpage.Forms.getByName("MainForm")
	DocCtl = ThisComponent.getCurrentController()
	oField = oForm.getByName("txtURL")
    oButton = oForm.getByName("Push Button 1")
  	CtlView = DocCtl.GetControl(oField)
    oButton.TargetUrl = CtlView.Text
end sub

attached to After record change event of form and on the push button property Action set to Open document/web page you only need a click of the button to open the link. Works well for opening other documents as well.

No need to use Shell.

1 Like

The even better event to execute the macro would be the “get focus” of the push_button itself
This executes the macro only when actually needed (and reads always the actual displayed content of the text field)

1 Like