Access send mail from access 2010

Joined
Feb 17, 2012
Messages
4
Reaction score
0
Hi,

I am trying to use this code in access 2010 to send a simple test mail

Public Function cmdMail()
Dim objOutlook As Outlook.Application
Dim objNameSpace As Outlook.NameSpace
Dim objMailItem As Outlook.MailItem
Dim objRecipient As Outlook.Recipient
Set objOutlook = New Outlook.Application
Set objNameSpace = objOutlook.GetNamespace("MAPI")
Set objMailItem = objOutlook.CreateItem(olMailItem)
Set objRecipient = objMailItem.Recipients.Add("me")
objRecipient.Type = olTo
Set objRecipient = objMailItem.Recipients.Add("you")
objRecipient.Type = olCC
objMailItem.Subject = "June 2002 Financial Analysis"
objMailItem.Body = "Hi, hope all's well. Here's the financial analysis. Regards JP"
objNameSpace.LogOn , , True
objMailItem.Display
Set objRecipient = Nothing
Set objMailItem = Nothing
Set objNameSpace = Nothing
Set objOutlook = Nothing
End Function

however when i step through it i get an application deined or object defined error at this line

Set objRecipient = objMailItem.Recipients.Add("me")
could someone help me please
 
Joined
Sep 3, 2008
Messages
164
Reaction score
5
Hi,

"me" is not defined.

Add this to your declarations:
Dim strMe as string
strMe = "(e-mail address removed)"

Change your statement to:
Set objRecipient = objMailItem.Recipients.Add(strMe)

Do the same for "you"

Stoneboysteve
 
Joined
Sep 3, 2008
Messages
164
Reaction score
5
replace E-Mail Removed with your email address. The syntax is strMe = quote Email address end quote. No parenthesis.

Steve
 
Joined
Feb 17, 2012
Messages
4
Reaction score
0
Steve,

I have changed my code to this now


Public Function cmdMail()
Dim objOutlook As Outlook.Application
Dim objNameSpace As Outlook.NameSpace
Dim objMailItem As Outlook.MailItem

Dim objRecipient As Outlook.Recipient
Dim strMe As String
strMe = "(e-mail address removed)"

Dim strYou As String
strYou = "(e-mail address removed)"

Set objOutlook = New Outlook.Application
Set objNameSpace = objOutlook.GetNamespace("MAPI")
Set objMailItem = objOutlook.CreateItem(olMailItem)

Set objRecipient = objMailItem.Recipients.Add(strMe)
objRecipient.Type = olTo

Set objRecipient = objMailItem.Recipients.Add(strYou)
objRecipient.Type = olCC

objMailItem.Subject = "Test"
objMailItem.Body = "Testing Again"
objNameSpace.LogOn , , True
objMailItem.Display
Set objRecipient = Nothing
Set objMailItem = Nothing
Set objNameSpace = Nothing
Set objOutlook = Nothing
End Function


and I still get the application defined or object defined error at this line

Set objRecipient = objMailItem.Recipients.Add(strMe)

Regards

Paul
 
Joined
Feb 17, 2012
Messages
4
Reaction score
0
Steve, sorry post seems to have got screwed this is what I have now

Public Function cmdMail()
Dim objOutlook As Outlook.Application
Dim objNameSpace As Outlook.NameSpace
Dim objMailItem As Outlook.MailItem
Dim objRecipient As Outlook.Recipient
Dim strMe As String
strMe = "(e-mail address removed)"
Dim strYou As String
strYou = "(e-mail address removed)"
Set objOutlook = New Outlook.Application
Set objNameSpace = objOutlook.GetNamespace("MAPI")
Set objMailItem = objOutlook.CreateItem(olMailItem)
Set objRecipient = objMailItem.Recipients.Add(strMe)
objRecipient.Type = olTo
Set objRecipient = objMailItem.Recipients.Add(strYou)
objRecipient.Type = olCC
objMailItem.Subject = "Test"
objMailItem.Body = "Testing Again"
objNameSpace.LogOn , , True
objMailItem.Display
Set objRecipient = Nothing
Set objMailItem = Nothing
Set objNameSpace = Nothing
Set objOutlook = Nothing
End Function

and I still get the application defined or object defined error at this line

Set objRecipient = objMailItem.Recipients.Add(strMe)

Regards

Paul
 
Joined
Sep 3, 2008
Messages
164
Reaction score
5
Paul,

If you are still having issues, try 'with'. I am leaving for a trip to the capital but the psudocode looks like:

with objMailitem
.recipient - ...
.recipient type = olto...
etc

end with.

Steve
 

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