Solved

Sending email via VBA code

Posted on 2006-07-13
15
337 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
ID: 17100420
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
ID: 17100431
Thanks, but what is the objects I need to use?
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 17100476
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
ID: 17100489
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
ID: 17100504
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
ID: 17100514
Do you not use outlook as your email system then?
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 17100660
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 4

Expert Comment

by:Carl2002
ID: 17100675
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
ID: 17101428
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
ID: 17101596
s/w = software  :-)

0
 

Author Comment

by:linder76
ID: 17101615
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
ID: 17101672
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
ID: 17101811
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
ID: 17103929
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
ID: 17103954
CDO = Collaboration Data Objects
its a MS thing
Remember to specify the FROM and SMTP Server also

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help writing a query 6 73
Open CSV, modify and save as xls from Access 12 19
ms access 2013, running .mdb 2 31
Acc 2010 pause execution 10 9
This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
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…

895 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

16 Experts available now in Live!

Get 1:1 Help Now