Page 1 of 1

create email hyperlink in form

PostPosted: Fri Feb 22, 2008 3:45 pm
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

Re: create email hyperlink in form

PostPosted: Fri Feb 22, 2008 7:12 pm
by QuazzieEvil
try the following code:
Code: Select all   Expand viewCollapse view
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.

Re: create email hyperlink in form

PostPosted: Sat Mar 08, 2008 11:54 pm
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

Re: create email hyperlink in form

PostPosted: Sun Mar 09, 2008 12:37 am
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   Expand viewCollapse view
HandleError:
  If err<>0 Then
    Exit Sub
  End If

Re: create email hyperlink in form

PostPosted: Sun Mar 09, 2008 12:53 am
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   Expand viewCollapse view
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   Expand viewCollapse view
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.