Emailing from Access

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have created a button in a form which i would like to send an email to the
address in an email field on the same form when clicked.
In my hunt to find code i have found this

email.to = "recipient address"
email.From = "SenderAddress"
email.Body = "MessageText"
email.Subject = "SubjectText"
email.BodyFormat = Web.Mail.MailFormat.Text
System.Web.Mail.SmtpMail.SmtpServer = "SmtpServerName"
System.Web.Mail.SmtpMail.Send (email)

I assume my email.to will be [formname].[emailaddress] {or maybe not}
but i cannot think what to put in for email.from as i would just like it to
use the account of the person logged in at the time.

If somebody could tell me if i am, firstly on the correct path and secondly,
how to complete the code; it would be much appreciated.
 
Fab, try this one:
Note: Do not forget to include "Microsoft Outlook Library" in references...

Sub SendMessage(aTo As String, aCc As String, aBcc As String, aSubject As
String, aBody As String, aHTMLBody As String, SendImmeditely As Boolean)
Dim objOutLook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment

' Create the Outlook session.
Set objOutLook = CreateObject("Outlook.Application")

' Create the message.
Set objOutlookMsg = objOutLook.CreateItem(olMailItem)

With objOutlookMsg
' Add the To recipient(s) to the message.
Set objOutlookRecip = .Recipients.Add(aTo)
objOutlookRecip.Type = olTo

' Add the CC recipient(s) to the message.
If aCc <> "" Then
Set objOutlookRecip = .Recipients.Add(aCc)
objOutlookRecip.Type = olCC
End If

' Add the BCC recipient(s) to the message.
If aBcc <> "" Then
Set objOutlookRecip = .Recipients.Add(aBcc)
objOutlookRecip.Type = olBCC
End If

' Set the Subject, Body, and Importance of the message.
.Subject = aSubject
If aHTMLBody = "" Then
.Body = aBody
Else
.HTMLBody = aHTMLBody
End If

.Importance = olImportanceNormal

' Resolve each Recipient's name.
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
Next

' Should we display the message before sending?
If SendImmeditely Then
.Save
.Send
Else
.Display
End If
End With
Set objOutLook = Nothing
End Sub

Take care
Mauricio Silva
 
Dear Mauricio:

Your method looks quite helpful to me. But, when you mean "include MS
Outlook Libray"= go to Tools --> Reference--> Choose Outlook x.0 Object Model
--> MS DAO 3.x Object Library???
(Sorry, I have Access 2003, I can't even see the option of Reference under
Tools menu)

Also, this subroutine should be added to the event of On click of Form, is
that correct?
Using this method will have all values of fields send to the person in mail
lists (which will be the subform in this main form)?

Thanks.

Vivi

Mauricio Silva said:
Fab, try this one:
Note: Do not forget to include "Microsoft Outlook Library" in references...

Sub SendMessage(aTo As String, aCc As String, aBcc As String, aSubject As
String, aBody As String, aHTMLBody As String, SendImmeditely As Boolean)
Dim objOutLook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment

' Create the Outlook session.
Set objOutLook = CreateObject("Outlook.Application")

' Create the message.
Set objOutlookMsg = objOutLook.CreateItem(olMailItem)

With objOutlookMsg
' Add the To recipient(s) to the message.
Set objOutlookRecip = .Recipients.Add(aTo)
objOutlookRecip.Type = olTo

' Add the CC recipient(s) to the message.
If aCc <> "" Then
Set objOutlookRecip = .Recipients.Add(aCc)
objOutlookRecip.Type = olCC
End If

' Add the BCC recipient(s) to the message.
If aBcc <> "" Then
Set objOutlookRecip = .Recipients.Add(aBcc)
objOutlookRecip.Type = olBCC
End If

' Set the Subject, Body, and Importance of the message.
.Subject = aSubject
If aHTMLBody = "" Then
.Body = aBody
Else
.HTMLBody = aHTMLBody
End If

.Importance = olImportanceNormal

' Resolve each Recipient's name.
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
Next

' Should we display the message before sending?
If SendImmeditely Then
.Save
.Send
Else
.Display
End If
End With
Set objOutLook = Nothing
End Sub

Take care
Mauricio Silva

Fab said:
I have created a button in a form which i would like to send an email to the
address in an email field on the same form when clicked.
In my hunt to find code i have found this

email.to = "recipient address"
email.From = "SenderAddress"
email.Body = "MessageText"
email.Subject = "SubjectText"
email.BodyFormat = Web.Mail.MailFormat.Text
System.Web.Mail.SmtpMail.SmtpServer = "SmtpServerName"
System.Web.Mail.SmtpMail.Send (email)

I assume my email.to will be [formname].[emailaddress] {or maybe not}
but i cannot think what to put in for email.from as i would just like it to
use the account of the person logged in at the time.

If somebody could tell me if i am, firstly on the correct path and secondly,
how to complete the code; it would be much appreciated.
 
Back
Top