Results 1 to 2 of 2

Thread: VB6 Code Exiting For Next Statement Early

  1. #1

    Thread Starter
    Hyperactive Member Jonny1409's Avatar
    Join Date
    Mar 2005
    Posts
    308

    VB6 Code Exiting For Next Statement Early

    Hello, I wonder if anyone can help me with a problem I'm having please.

    I have some code which is basically using a timer and checking an Outlook mailbox, and if there is an unread mail in there with an attachement, it strips the attachement out and saves it to a file location.

    Code:
    For Each item In InBox.Items
                vvalid = False
                If item.Attachments.Count > 0 And item.UnRead Then
                    For Each Atmt In item.Attachments
                            txt1.Text = "PROCESSING FILE..."  
                            Set fso = New FileSystemObject
                            tempfilename = Atmt.FileName
                            tempfilename = Replace(tempfilename, " ", "_")
                            If fso.FolderExists(directory) Then
                                FileName = directory & tempfilename
                                Atmt.SaveAsFile FileName
                            Else
                                ErrorHandler "Public Sub ErrorHandling"
                                vvalid = False
                            End If
                            Set fso = Nothing                                                       
                            vvalid = True
                    Next Atmt
                    If vvalid = True Then
                        item.UnRead = False
                        item.Move olMoveFolder
                    End If     
                End If
    Next item
    This all works fine and works very quicly.

    However, if there are say 20 e-mails in the inbox the first time the code runs it will process 10 before exiting out of the code rather than processing all 20.

    It will then wait a minute until the next timer event ticks over and it will process 4 more.

    Initially I thought it was doing this because of the fact that the timer event was ticking over part way through the process, but if that were the case then surely the 2nd time it would also process 10 files?

    I need it to process the whole number of files on the first run through, and I can't understand why it's coming out of the code.

    Can anyone shed a light on this please?

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VB6 Code Exiting For Next Statement Early

    if you remove items within your loop the indexes get changed and the next item is skipped
    try using a reverse loop
    vb Code:
    1. for i = inbox.items.count - 1 to 0 step -1
    2.   set item = inbox.items(i)
    3.   If item.Attachments.Count > 0 And itemsUnRead Then
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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