CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2001
    Posts
    1

    Error handler fails

    I have a error handler in a "FOR,NEXT" loop, it do opens a file system object and writes the error into a log file.The issue is I did't set the "EXIt sub" but i specified the handler to go to the "next" statement because i dont want the error handler to exit sub. it does go to the for loop again runs the loop,if i have an error again the handler FAILS...

    Guys if u got anything Please reply me back, Any help will be greatly appreciated

    "Ainaken SARAN PARAMATMUNI"


    The Code Follows :

    Private Sub Command2_Click()

    MAPISession1.SignOn

    MAPIMessages1.SessionID = MAPISession1.SessionID
    MAPIMessages1.Fetch

    For i = 0 To MAPIMessages1.MsgCount - 1
    On Error GoTo logerr
    MAPIMessages1.MsgIndex = i
    If Not MAPIMessages1.MsgRead = True Then
    If Not Len(MAPIMessages1.MsgSubject) = 0 Then
    strSubject = MAPIMessages1.MsgSubject
    Else
    strSubject = MAPIMessages1.MsgOrigDisplayName
    End If
    If Not Len(MAPIMessages1.MsgNoteText) = 0 Then
    strNoteTxt = MAPIMessages1.MsgNoteText
    End If
    If Not Len(MAPIMessages1.MsgOrigDisplayName) = 0 Then
    strFromAdd = MAPIMessages1.MsgOrigDisplayName
    End If

    Set file = fso.CreateTextFile("C:\Documents and Settings\MythelS1\Desktop\EmailSource\" & strSubject & ".txt", True)
    file.Close
    Set Getfile = fso.Getfile("C:\Documents and Settings\MythelS1\Desktop\EmailSource\" & strSubject & ".txt")
    Set writefile = Getfile.OpenAsTextStream(ForWriting)
    With writefile
    .Write strNoteTxt
    .Close
    End With
    Command2.Enabled = True
    End If
    Command2.Enabled = False
    MAPISession1.NewSession = True
    99: Next
    MAPISession1.SignOff

    '----ERROR HANDLER-----

    logerr:

    If Not Err = 0 Then
    logerror = Err.Description & vbNewLine & _
    "Error" & Err.Description & " occurred in writing down the textfile" & vbNewLine & _
    strFromAdd & vbNewLine & vbNewLine & strSubject & vbNewLine & Now

    Set Logfile = fso.OpenTextFile("C:\Documents and Settings\MythelS1\Desktop\log.txt", ForWriting)

    With Logfile
    .Write logerror
    .Close
    End With
    Err.Clear
    GoTo 99
    End If
    End Sub



  2. #2
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: Error handler fails

    Your code is real chowmein. You could have structured it a little bit better.

    Try putting the On Error Goto statement outside the For loop. Maybe it works.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured