Solved

Error Trapping

Posted on 2002-06-11
4
372 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
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

816 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

7 Experts available now in Live!

Get 1:1 Help Now