473,387 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Email with Outlook from Access

Hi,

I've recently set up a database, but have not experience of VBA and
need to find a way of solving this problem.

Basically, I would like to make a form with two buttons. On clicking
one button an input box should appear asking for an email address and
then an email should be sent to that address with an attachement. The
other button should send an email to all the addresses in the email
address field in a table.

I've been trying to work with some code I've found on the internet, but
I can't seem to implement it properly.

This is what I've been working with:

http://www.mvps.org/access/modules/mdl0019.htm

'**************** Usage Example Start ****************
Sub TestMAPIEmail()
Dim clMAPI As clsMAPI
Set clMAPI = New clsMAPIEmail
With clMAPI
.MAPILogon
.MAPIAddMessage
.MAPISetMessageBody = "Test Message"
.MAPISetMessageSubject = "Some Test"
.MAPIAddRecipient stPerson:="da****@hotmail.com", _
intAddressType:=1 'To
.MAPIAddRecipient stPerson:="Dev Ashish", _
intAddressType:=2 'cc
.MAPIAddRecipient stPerson:="smtp:da****@hotmail.com", _
intAddressType:=3 'bcc

.MAPIAddAttachment "C:\temp\Readme.doc", "Jet Readme"
.MAPIAddAttachment stFile:="C:\config.sys"

.MAPIUpdateMessage
.MAPISendMessage boolSaveCopy:=False
.MAPILogoff
End With
End Sub
'**************** Usage Example End ****************

'**************** Class Start ***********************
'This code was originally written by Dev Ashish
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code Courtesy of
'Dev Ashish
'
Option Compare Database
Option Explicit

Private mobjSession As MAPI.Session
Private mobjMessage As Message
Private mboolErr As Boolean
Private mstStatus As String
Private mobjNewMessage As Message

Private Const mcERR_DOH = vbObjectError + 10000
Private Const mcERR_DECIMAL = 261144 'low word order +1000

Public Sub MAPIAddMessage()
With mobjSession
Set mobjNewMessage = .Outbox.Messages.Add
End With
End Sub

Public Sub MAPIUpdateMessage()
mobjNewMessage.Update
End Sub

Private Sub Class_Initialize()
mboolErr = False
End Sub

Private Sub Class_Terminate()
On Error Resume Next
Set mobjMessage = Nothing
mobjSession.Logoff
Set mobjSession = Nothing
End Sub

Public Property Let MAPISetMessageBody(stBodyText As String)
If Len(stBodyText) 0 Then mobjNewMessage.Text = stBodyText
End Property

Public Property Let MAPISetMessageSubject(stSubject As String)
If Len(stSubject) 0 Then mobjNewMessage.Subject = stSubject
End Property

Public Property Get MAPIIsError() As Boolean
MAPIIsError = mboolErr
End Property

Public Property Get MAPIRecipientCount() As Integer
MAPIRecipientCount = mobjNewMessage.Recipients.Count
End Property

Public Sub MAPIAddAttachment(stFile As String, _
Optional stLabel As Variant)
Dim objAttachment As Attachment
Dim stMsg As String

On Error GoTo Error_MAPIAddAttachment

If mboolErr Then Err.Raise mcERR_DOH
If Len(Dir(stFile)) = 0 Then Err.Raise mcERR_DOH + 10

mstStatus = SysCmd(acSysCmdSetStatus, "Adding Attachments...")

If IsMissing(stLabel) Then stLabel = CStr(stFile)

With mobjNewMessage
.Text = " " & mobjNewMessage.Text
Set objAttachment = .Attachments.Add
With objAttachment
.Position = 0
.Name = stLabel
'no need to link a file me thinks
.Type = CdoFileData
.ReadFromFile stFile
End With
.Update
End With

Exit_MAPIAddAttachment:
Set objAttachment = Nothing
Exit Sub
Error_MAPIAddAttachment:
mboolErr = True
If Err = mcERR_DOH + 10 Then
stMsg = "Couldn't locate the file " & vbCrLf
stMsg = stMsg & "'" & stFile & "'." & vbCrLf
stMsg = stMsg & "Please check the file name and path and try
again."
MsgBox stMsg, vbExclamation + vbOKOnly, "File Not Found"
ElseIf Err <mcERR_DOH Then
MsgBox "Error " & Err.Number & vbCrLf & Err.Description
End If
Resume Exit_MAPIAddAttachment
End Sub

Public Sub MAPIAddRecipient(stPerson As String, intAddressType As
Integer)
Dim objNewRecipient As Recipient 'local

On Error GoTo Error_MAPIAddRecipient
mstStatus = SysCmd(acSysCmdSetStatus, "Adding Recipients...")

If mboolErr Then Err.Raise mcERR_DOH

'If there's no SMTP present in the stPerson var, then
'we have to use Name, else Address
With mobjNewMessage
If InStr(1, stPerson, "SMTP:") 0 Then
Set objNewRecipient = .Recipients.Add(Address:=stPerson, _
Type:=intAddressType)
Else
Set objNewRecipient = .Recipients.Add(Name:=stPerson, _
Type:=intAddressType)
End If
objNewRecipient.Resolve
End With

Exit_MAPIAddRecipient:
Set objNewRecipient = Nothing
Exit Sub

Error_MAPIAddRecipient:
mboolErr = True
Resume Exit_MAPIAddRecipient
End Sub

Public Sub MAPISendMessage(Optional boolSaveCopy As Variant, _
Optional boolShowDialog As Variant)

mstStatus = SysCmd(acSysCmdSetStatus, "Sending message...")
If IsMissing(boolSaveCopy) Then
boolSaveCopy = True
End If
If IsMissing(boolShowDialog) Then
boolShowDialog = False
End If

mobjNewMessage.Send savecopy:=boolSaveCopy,
showdialog:=boolShowDialog
End Sub

Public Sub MAPILogon()
On Error GoTo err_sMAPILogon
Const cERROR_USERCANCEL = -2147221229

mstStatus = SysCmd(acSysCmdSetStatus, "Login....")
Set mobjSession = CreateObject("MAPI.Session")
mobjSession.Logon

exit_sMAPILogon:
Exit Sub

err_sMAPILogon:
mboolErr = True
If Err = CdoE_LOGON_FAILED - mcERR_DECIMAL Then
MsgBox "Logon Failed", vbCritical + vbOKOnly, "Error"
ElseIf Err = cERROR_USERCANCEL Then
MsgBox "Aborting since you pressed cancel.", _
vbOKOnly + vbInformation, "Operatoin Cancelled!"
Else
MsgBox "Error number " & Err - mcERR_DECIMAL & " description. "
_
& Error$(Err)
End If
Resume exit_sMAPILogon
End Sub

Public Sub MAPILogoff()
On Error GoTo err_sMAPILogoff
mstStatus = SysCmd(acSysCmdSetStatus, "Logging off...")
mobjSession.Logoff

Set mobjNewMessage = Nothing
Set mobjSession = Nothing
mstStatus = SysCmd(acSysCmdClearStatus)
exit_sMAPILogoff:
Exit Sub

err_sMAPILogoff:
Resume exit_sMAPILogoff
End Sub
'**************** Class End ***********************

I also found some code on the microsoft website that looked promising,
but again, I'm not sure how to exactly implement it and get it to do
what I want. The code on the Microsoft website is here:

http://support.microsoft.com/?kbid=209948

I would be really grateful for any help! Thanks so much!

Ben

Oct 20 '06 #1
1 2091
Have you tried using the sendobject method? It works quite well for
me. It allows the sending of objects via your default email program.
Go into a new module and hit F1 and search on sendobject. It gives you
all of the particulars.

Cyberwolf
da********@gmail.com wrote:
Hi,

I've recently set up a database, but have not experience of VBA and
need to find a way of solving this problem.

Basically, I would like to make a form with two buttons. On clicking
one button an input box should appear asking for an email address and
then an email should be sent to that address with an attachement. The
other button should send an email to all the addresses in the email
address field in a table.

I've been trying to work with some code I've found on the internet, but
I can't seem to implement it properly.

This is what I've been working with:

http://www.mvps.org/access/modules/mdl0019.htm

'**************** Usage Example Start ****************
Sub TestMAPIEmail()
Dim clMAPI As clsMAPI
Set clMAPI = New clsMAPIEmail
With clMAPI
.MAPILogon
.MAPIAddMessage
.MAPISetMessageBody = "Test Message"
.MAPISetMessageSubject = "Some Test"
.MAPIAddRecipient stPerson:="da****@hotmail.com", _
intAddressType:=1 'To
.MAPIAddRecipient stPerson:="Dev Ashish", _
intAddressType:=2 'cc
.MAPIAddRecipient stPerson:="smtp:da****@hotmail.com", _
intAddressType:=3 'bcc

.MAPIAddAttachment "C:\temp\Readme.doc", "Jet Readme"
.MAPIAddAttachment stFile:="C:\config.sys"

.MAPIUpdateMessage
.MAPISendMessage boolSaveCopy:=False
.MAPILogoff
End With
End Sub
'**************** Usage Example End ****************

'**************** Class Start ***********************
'This code was originally written by Dev Ashish
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code Courtesy of
'Dev Ashish
'
Option Compare Database
Option Explicit

Private mobjSession As MAPI.Session
Private mobjMessage As Message
Private mboolErr As Boolean
Private mstStatus As String
Private mobjNewMessage As Message

Private Const mcERR_DOH = vbObjectError + 10000
Private Const mcERR_DECIMAL = 261144 'low word order +1000

Public Sub MAPIAddMessage()
With mobjSession
Set mobjNewMessage = .Outbox.Messages.Add
End With
End Sub

Public Sub MAPIUpdateMessage()
mobjNewMessage.Update
End Sub

Private Sub Class_Initialize()
mboolErr = False
End Sub

Private Sub Class_Terminate()
On Error Resume Next
Set mobjMessage = Nothing
mobjSession.Logoff
Set mobjSession = Nothing
End Sub

Public Property Let MAPISetMessageBody(stBodyText As String)
If Len(stBodyText) 0 Then mobjNewMessage.Text = stBodyText
End Property

Public Property Let MAPISetMessageSubject(stSubject As String)
If Len(stSubject) 0 Then mobjNewMessage.Subject = stSubject
End Property

Public Property Get MAPIIsError() As Boolean
MAPIIsError = mboolErr
End Property

Public Property Get MAPIRecipientCount() As Integer
MAPIRecipientCount = mobjNewMessage.Recipients.Count
End Property

Public Sub MAPIAddAttachment(stFile As String, _
Optional stLabel As Variant)
Dim objAttachment As Attachment
Dim stMsg As String

On Error GoTo Error_MAPIAddAttachment

If mboolErr Then Err.Raise mcERR_DOH
If Len(Dir(stFile)) = 0 Then Err.Raise mcERR_DOH + 10

mstStatus = SysCmd(acSysCmdSetStatus, "Adding Attachments...")

If IsMissing(stLabel) Then stLabel = CStr(stFile)

With mobjNewMessage
.Text = " " & mobjNewMessage.Text
Set objAttachment = .Attachments.Add
With objAttachment
.Position = 0
.Name = stLabel
'no need to link a file me thinks
.Type = CdoFileData
.ReadFromFile stFile
End With
.Update
End With

Exit_MAPIAddAttachment:
Set objAttachment = Nothing
Exit Sub
Error_MAPIAddAttachment:
mboolErr = True
If Err = mcERR_DOH + 10 Then
stMsg = "Couldn't locate the file " & vbCrLf
stMsg = stMsg & "'" & stFile & "'." & vbCrLf
stMsg = stMsg & "Please check the file name and path and try
again."
MsgBox stMsg, vbExclamation + vbOKOnly, "File Not Found"
ElseIf Err <mcERR_DOH Then
MsgBox "Error " & Err.Number & vbCrLf & Err.Description
End If
Resume Exit_MAPIAddAttachment
End Sub

Public Sub MAPIAddRecipient(stPerson As String, intAddressType As
Integer)
Dim objNewRecipient As Recipient 'local

On Error GoTo Error_MAPIAddRecipient
mstStatus = SysCmd(acSysCmdSetStatus, "Adding Recipients...")

If mboolErr Then Err.Raise mcERR_DOH

'If there's no SMTP present in the stPerson var, then
'we have to use Name, else Address
With mobjNewMessage
If InStr(1, stPerson, "SMTP:") 0 Then
Set objNewRecipient = .Recipients.Add(Address:=stPerson, _
Type:=intAddressType)
Else
Set objNewRecipient = .Recipients.Add(Name:=stPerson, _
Type:=intAddressType)
End If
objNewRecipient.Resolve
End With

Exit_MAPIAddRecipient:
Set objNewRecipient = Nothing
Exit Sub

Error_MAPIAddRecipient:
mboolErr = True
Resume Exit_MAPIAddRecipient
End Sub

Public Sub MAPISendMessage(Optional boolSaveCopy As Variant, _
Optional boolShowDialog As Variant)

mstStatus = SysCmd(acSysCmdSetStatus, "Sending message...")
If IsMissing(boolSaveCopy) Then
boolSaveCopy = True
End If
If IsMissing(boolShowDialog) Then
boolShowDialog = False
End If

mobjNewMessage.Send savecopy:=boolSaveCopy,
showdialog:=boolShowDialog
End Sub

Public Sub MAPILogon()
On Error GoTo err_sMAPILogon
Const cERROR_USERCANCEL = -2147221229

mstStatus = SysCmd(acSysCmdSetStatus, "Login....")
Set mobjSession = CreateObject("MAPI.Session")
mobjSession.Logon

exit_sMAPILogon:
Exit Sub

err_sMAPILogon:
mboolErr = True
If Err = CdoE_LOGON_FAILED - mcERR_DECIMAL Then
MsgBox "Logon Failed", vbCritical + vbOKOnly, "Error"
ElseIf Err = cERROR_USERCANCEL Then
MsgBox "Aborting since you pressed cancel.", _
vbOKOnly + vbInformation, "Operatoin Cancelled!"
Else
MsgBox "Error number " & Err - mcERR_DECIMAL & " description. "
_
& Error$(Err)
End If
Resume exit_sMAPILogon
End Sub

Public Sub MAPILogoff()
On Error GoTo err_sMAPILogoff
mstStatus = SysCmd(acSysCmdSetStatus, "Logging off...")
mobjSession.Logoff

Set mobjNewMessage = Nothing
Set mobjSession = Nothing
mstStatus = SysCmd(acSysCmdClearStatus)
exit_sMAPILogoff:
Exit Sub

err_sMAPILogoff:
Resume exit_sMAPILogoff
End Sub
'**************** Class End ***********************

I also found some code on the microsoft website that looked promising,
but again, I'm not sure how to exactly implement it and get it to do
what I want. The code on the Microsoft website is here:

http://support.microsoft.com/?kbid=209948

I would be really grateful for any help! Thanks so much!

Ben
Oct 20 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jay McGrath | last post by:
Help - trying to send a simple text email with with as little user intervention. I am trying to create a button in my Access application that will automatically send a simple text email. It...
1
by: Devonish | last post by:
I am composing an email with Access VB and then sending it from within Access. Everything works correctly (the email actually goes!) but Outlook ask some irritating questions that the user is...
7
by: Daven Thrice | last post by:
I know how to send email out of Access, and that's no problem. What I need to figure out is how to receive email into access. Say that I give access an email address, such as access@daven.com. I...
17
by: Bonj | last post by:
Right guys. (I would like a solution to this in VB6 as this is what our needy app is written in, but any solutions that involve .NET would be much appreciated likewise as I could instantiate...
5
by: J-P-W | last post by:
I have some code, from this group (many thanks) that sends an attachment to an email. The following: Dim objNewMail As Outlook.MailItem Dim golApp As...
4
by: JJ | last post by:
Whats the most compatilbe way of sending an email ?- I need to consider that the OS may be win 98/win NT/Win 2000/winXP. I was constructing a mailto command, but the contents of the text file I...
5
by: bobdydd | last post by:
Hi Everbody Access 2000, Windows XP, Outlook 2000, When I open my database it is set to attach to my Outlook 2000 Inbox. So far so good..... The attaching procedure works OK and I am able to...
10
by: Walshi | last post by:
Hi all, I'm a relative newby to access and VBA etc. My forms and tables etc are working great and saving lots of time...However... I have two databases with the exact same table format. I want...
1
by: seanhirshberg | last post by:
I have code that current sends spreadsheets by email from a user's Outlook. I need to modify the code to send the email from their group email box, so the recipient can respond to the group email....
1
by: Paul Brady | last post by:
This must be a common operation, but I have searched this newsgroup and have wrestled with the help messages, and I seem to need help with this simple task. I have an Access 2000 database which,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.