Solved

Error Trapping

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

696 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