Solved

Sending email via VBA code

Posted on 2006-07-13
15
321 Views
Last Modified: 2008-02-01
I am writing a error routine for a program. My client wants me to write where if the program errors during its run that the error is sent to her cell phone (text message) or email account. Is there a way to do this?

Thank you,

Joe
0
Comment
Question by:linder76
  • 7
  • 4
  • 3
  • +1
15 Comments
 
LVL 4

Expert Comment

by:Carl2002
Comment Utility
I'm not sure on the test message front but you can definately do it via email.

try:

DoCmd.SendObject [objecttype][, objectname][, outputformat][, to][, cc][, bcc][, subject][, messagetext][, editmessage][, templatefile]

Carl.
0
 

Author Comment

by:linder76
Comment Utility
Thanks, but what is the objects I need to use?
0
 
LVL 65

Expert Comment

by:rockiroads
Comment Utility
By far the simplest is SendObject
if u use this, then credit goes to Carl
this is an example of using it

DoCmd.SendObject , , , "fred@smith.com", , , "My Subject", "My Message - Oh Dear"



There are alternative methods also

Do note, if u have the later versions of Outlook, then u may well get a security message coming up
If u do then look at the answer I gave to, of all people, Carl!   http://www.experts-exchange.com/Databases/MS_Access/Q_21904604.html

Note, if u use SendObject, credit goes to Carl, not me

0
 

Author Comment

by:linder76
Comment Utility
This needs to work without Outlook. It needs to send the message via the code with out user intervention. Is that possible?
0
 
LVL 65

Accepted Solution

by:
rockiroads earned 500 total points
Comment Utility
If u install that s/w called Click Yes, then that may do it for u - no user intervention

DoCmd.SendObjects will use Outlook if u have that installed


If u know your SMTP address, u can use CDO


Public Function SendEmailCDO(ByVal strTo As String, _
                          ByVal strMessage As String, _
                          ByVal strSubject As String, _
                          Optional ByVal strAttach As String)

    Dim objEmail As Object
   
   
    On Error Resume Next
   
    Set objEmail = CreateObject("CDO.Message")
    '**** email address of sender
    objEmail.From = "fred@smith.com"      
    objEmail.To = strTo
    objEmail.Subject = strSubject
    objEmail.TextBody = strMessage
    if strAttach <> "" then objEmail.AddAttachment strAttach
    objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

    '**** smtp.xxx.com - here u enter your smtp server name, whatever that is
    objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.xx.com"

    objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    objEmail.Configuration.Fields.Update
    objEmail.Send
    If Err.Number <> 0 Then
        MsgBox "Error in sending. " & Err.Description
    Else
        MsgBox "Sent"   'remove this if u dont want confirmation
    End If
    Set objEmail = Nothing

End Function
0
 
LVL 4

Expert Comment

by:Carl2002
Comment Utility
Do you not use outlook as your email system then?
0
 
LVL 65

Expert Comment

by:rockiroads
Comment Utility
Carl, who was that question to?

if to me, well then, CDO uses Exchange directly, but I use both Outlook and CDO. I prefer CDO if and when I know the SMTP server, otherwise stick to SendObject or Outlook Auto
I prefer CDO only because of the security check it bypasses
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 4

Expert Comment

by:Carl2002
Comment Utility
It was to linder76 rocki, but I understand completely why you prefer CDO if it bypasses security after my problem a few days ago
0
 

Author Comment

by:linder76
Comment Utility
Correct I donot want to use Outlook. This needs to be an automated event.. I am reviewing the comments, what does s/w stand for?
Joe
0
 
LVL 26

Expert Comment

by:dannywareham
Comment Utility
s/w = software  :-)

0
 

Author Comment

by:linder76
Comment Utility
I have copy and pasted the code. however the web address when I click on them don't work...also I erase them in the code. Should I be doing that?
0
 

Author Comment

by:linder76
Comment Utility
I am still haveing problems. I have my reference book with me. What does CDO stand for. I am not familar with it.
0
 

Author Comment

by:linder76
Comment Utility
I keep getting the following error...

Error in sending. The message could not be sent to the smtp server.

The transport error code was 0x80040217. The server response was not available.

Help????
0
 

Author Comment

by:linder76
Comment Utility
Thanks for your help! I finally found what I was doing wrong. I needed to add script lines for my account name and password

Joe
0
 
LVL 65

Expert Comment

by:rockiroads
Comment Utility
CDO = Collaboration Data Objects
its a MS thing
Remember to specify the FROM and SMTP Server also

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now