Create email hyperlink in form

Creating and using forms
Post Reply
dstockman
Posts: 38
Joined: Fri Feb 22, 2008 3:20 pm

Create email hyperlink in form

Post by dstockman »

How do I create an email hyperlink in a form? MS Access has a hyperlink data format that makes it easy.

I have a main form with patient demographics. One of the text boxes is for the patient's email address. I want to be able to open my email program by just clicking on the text (or an associated button) with the To: field in the email program already filled in. This sort of action is automatic from a Calc file. OO Calc automatically recognizes an email address entered into a cell so later I can just click on the email address and what I describe above happens.

Any help would be appreciated. Of note, I am just learning macro programming and am not very good at it yet.

Doug
QuazzieEvil
Volunteer
Posts: 283
Joined: Tue Dec 04, 2007 6:38 pm
Location: Houston, TX

Re: create email hyperlink in form

Post by QuazzieEvil »

try the following code:

Code: Select all

Sub openEmailClient(Event As Object)
	REM code bound to 'Mouse Button Pressed' of a Text Box
  	Dim MailClient As Object
  	Dim MailAgent As Object
  	Dim MailMessage As Object
	Dim mTo As String
	Dim UI As Integer
	If Event.Source.Text="" Then
		Exit Sub REM NO EMAIL ADDRESS	
	End If
  	mTo=Event.Source.Text

    MailAgent=CreateUnoService("com.sun.star.system.SimpleSystemMail")  
  	MailClient=MailAgent.querySimpleMailClient()
  	MailMessage=MailClient.createSimpleMailMessage()
	
  	MailMessage.setRecipient(mTo)
  	UI=0
  	REM the UI flag indicates if the mail client user interface is to be opened (0)
  	REM or send w/o opening (1).  If you select 1, your email client may open a confirmation box
  	REM indicating someone is trying to send an email--sometimes it is not the top most window.
  	REM if nothing seems to happen, your email client may be waiting for you to respond
  	REM you can set your email client to always allow an application to send emails--but may be
  	REM a security issue
  	MailClient.sendSimpleMailMessage(MailMessage, UI)
End Sub
to bind the code to your text box, open the properties dialog for the text box, select the 'events' tab, and then click on the button next to the 'mouse button pressed' event. now browse your macros libraries to whereever you saved this code.
dstockman
Posts: 38
Joined: Fri Feb 22, 2008 3:20 pm

Re: create email hyperlink in form

Post by dstockman »

QuazzieEvil:

Thanks very much! I have it working somewhat but in real use I often get crashes. If I decide to not send the email the Basic IDE opens with an error msg. Or if I need to edit the email address I cannot do so without getting a "crash". I assume I have to capture these errors in some way and have a more graceful notification. I guess another option would be to place a button ("Send") next to the email address in the text box. The only time I send an email is if I click on the send button. This would make editing the email address better/possible.
Doug
QuazzieEvil
Volunteer
Posts: 283
Joined: Tue Dec 04, 2007 6:38 pm
Location: Houston, TX

Re: create email hyperlink in form

Post by QuazzieEvil »

make the first line of the sub : On Error Goto HandleError
now, define the HandleError label after your current last line of code (line before the End Sub)

now define the lable and some code to go with it.

Code: Select all

HandleError:
  If err<>0 Then
    Exit Sub
  End If
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: create email hyperlink in form

Post by Villeroy »

:roll: This database is going to make me angry. The following works for me:
I have a form based on a query having a field of mail addresses named "E-Mail-Adresse" (German).
I added a button named "mailto" with Tag-property (additional information in the form designer) "E-Mail-Adresse".
Action is "Open document/web-page"
URL is empty.
The form's "After Record Change" event is assigned to the following Python macro in <ooo_user_dir>/user/Scripts/python/DBForms.py

Code: Select all

import uno

def on_record_change(oEv):
    frm = oEv.Source
    cols = frm.getColumns()
    btn = frm.getByName("mailto")
    tag = btn.Tag
    field = cols.getByName(tag)
    btn.TargetURL = 'mailto:'+ field.getString()
Same in Basic (untested)

Code: Select all

Sub on_record_change(oEv)
    frm = oEv.Source
    cols = frm.getColumns()
    btn = frm.getByName("mailto")
    tag = btn.Tag
    field = cols.getByName(tag)
    btn.TargetURL = "mailto:"& field.getString()
End Sub
After each record change the URL of button "mailto" is set to "mailto"+ <URL in the mail-field>
When you push the button, it should start your preferred mail client with the address.
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