VBA code to send email.

C

capinvest

Does anyone know how to code Access to send an email when
a certain condition is met. I am trying to automate our
billing system and would like access to send an email to a
specific address every time a client needs a bill sent to
them. If anyone can help by supplying a generic code, it
would be greatly appreciated.
 
G

Geoff

Alternatively, if you want to program Outlook
to send an email, you could do it like this:

Sub SendEmail()

' Assumes reference to Outlook Object Library
' (Set in vba editor, on Tools, References menu).

On Error GoTo HandleErrors

Dim objOL As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim objMI As Outlook.MailItem

Set objOL = New Outlook.Application
Set objNS = objOL.GetNamespace("MAPI")

' If using email server, allow user to logon
' (Ignored if already logged on or if not
' using server):
objNS.Logon , , True, False

Set objMI = objOL.CreateItem(olMailItem)
With objMI
.To = "Recipients Email Address Here" & ""
.CC = "Carbon Copies (Email Addresses) Here"
.BCC = "Blind Copies (Email Addresses) Here"
.Subject = "Subject Text Here"
.Body = "Message Text Here"
.Display
' Instead of displaying message,
' you could send immediately:
' .Send
End With

Bye:
Set objMI = Nothing
Set objNS = Nothing
Set objOL = Nothing
Exit Sub

HandleErrors:
MsgBox Err.Description, vbOKOnly, "Error No: " & CStr(Err.Number)
Resume Bye

End Sub


You may find it more convenient to pass the variable
data (eg To) to the subprocedure, eg

Sub SendEmail(varTo as Variant, etc., etc.)

Then later:

With objMI
.To = varTo & ""

etc, etc.

For further information:
www.slipstick.com

Regards
Geoff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top