Solved

Error Trapping

Posted on 2002-06-11
4
373 Views
Last Modified: 2012-05-04
I need to trap for a Runtime error 426 and when this happens I need an email send to test@test.com with the error code in the message.  Can some on send me some sample code to me with this this in it.

Thanks...
0
Comment
Question by:mtbuford
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 

Accepted Solution

by:
VoiceOfDissent earned 50 total points
ID: 7071142
That's fairly straightforward, assuming you have the code to send the email.  That code is available elsewhere, so I won't rip another's code off for you.  But here's the basics.

Sub SendEmail(strSubjText as String, strBodyText as String, strRecipient as String)

(email code goes here)

End Sub


In the procedure to trap for the error 426...

On Error Goto Error_handler
(your code here)


Error_handler:
if Err.Num = 426 then
     Call SendEmail("Error 426 in MyProg", Err.Description, "test@test.com")

Else
     response = msgbox(Err.Description, vbOkOnly, "Error " & Err.Num)

end if

Resume Next (or Resume (line name))

Hope this helps.  Good luck.


0
 
LVL 5

Expert Comment

by:rpai
ID: 7071148
The following lines of code uses Microsoft OutLook Library to send an email.

In you function where you want to Trap the Error 426 implement this

Public Sub SendEmailOnError()

On Error Goto ErrHandler
.
.
.
.
Exit Function
SendMessage
ErrHandler:

End Function


Public Function SendMessage() As Boolean
' The SendMessage() function reads user entered values and
' actually sends the message.

On Error Resume Next

Dim strRecip As String
Dim strSubject As String
Dim strMsg As String
Dim strAttachment As String

strSubject = "Error 426"
strRecip = "test@test.com "
strMsg = "Runtime error 426 generated"

' Any amount of validation could be done at this point, but
' at a minimum, you need to verify that the user supplied an
' Email address for a recipient.
If Len(strRecip) = 0 Then
    strMsg = "You must designate a recipient."
    MsgBox strMsg, vbExclamation, "Error"
    Exit Function
End If

' Assume success
fSuccess = True

' Here's where the real Outlook Automation takes place
If GetOutlook = True Then
    Set mItem = mOutlookApp.CreateItem(olMailItem)
    mItem.Recipients.Add strRecip
    mItem.Subject = strSubject
    mItem.Body = strMsg
   
    ' This code allows for 1 attachment, but with slight
    ' modification, you could provide for multiple files.
    If Len(strAttachment) > 0 Then
        mItem.Attachments.Add strAttachment
    End If
   
    mItem.Save
    mItem.Send
End If

' Release resources
Set mOutlookApp = Nothing
Set mNameSpace = Nothing

If Err.Number > 0 Then fSuccess = False
SendMessage = fSuccess
End Function


Private Function GetOutlook() As Boolean
' The GetOutlook() function sets the Outlook Application
' and Namespase objects and opens MS Outlook
On Error Resume Next

' Assume success
fSuccess = True

Set mOutlookApp = GetObject("", "Outlook.application")

' If Outlook is NOT Open, then there will be an error.
' Attempt to open Outlook
If Err.Number > 0 Then
    Err.Clear
    Set mOutlookApp = CreateObject("Outlook.application")
       
    If Err.Number > 0 Then
        MsgBox "Could not create Outlook object", vbCritical
        fSuccess = False
        Exit Function
    End If
End If

' If we've made it this far, we have an Outlook App Object
' Now, set the NameSpace object to MAPI Namespace
Set mNameSpace = mOutlookApp.GetNamespace("MAPI")
   
If Err.Number > 0 Then
    MsgBox "Could not create NameSpace object", vbCritical
    fSuccess = False
    Exit Function
End If

' Return the Success Flag as the value of GetOutlook()
GetOutlook = fSuccess
   
End Function
0
 
LVL 5

Expert Comment

by:rpai
ID: 7071285
Ooops!
Should be :-
On Error Goto ErrHandler
.
.
.
.
Exit Function

ErrHandler:
SendMessage
End Function

And the rest of the code as posted above should work.
0
 
LVL 5

Expert Comment

by:rpai
ID: 7084132
Please maintain your open questions.
Questions Asked 83
Last 10 Grades Given A A A A C A B A A A  
Question Grading Record 66 Answers Graded / 66 Answers Received
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

730 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