Search inbox, then remove found message with vba

Hello. I’m new to VBA (but have gotten pretty good with it) and just recently I tackled trying to search through our Lotus Notes Inbox for certain messages (by subject), save the attachments, then move the messages out of the Inbox. I accomplished all this, however, I can not for the life of me figure out how to remove the messages from the Inbox.

My problem with this derives from how I’m searching through the Inbox I believe.

do until oDocument is Nothing

…then look at each email individually, match, then remove

loop

This is causing the code to miss certain emails, because not every email is being removed. In some way, when a message is removed, the NextDocument moves more than just to the next document.

The only way I have found around this is to loop until some set number…that is set really high. That wastes cycles, and I will never really know how many emails are going to be in the Inbox. Taking count of all emails, then looping to that count still misses emails (read above paragraph). And resetting oDocument when it hits Nothing gets stuck in an infiniate Loop because not emails are being removed.

Here is the code I’m working with:

Set oDocument = oView.getfirstdocument

Do Until oDocument Is Nothing

    stSubject = oDocument.GetItemValue("Subject")(0) ' sets email's subject line

    stSubject = Trim(stSubject) ' removes any leading/trailing whitespace



    For Each iU In sMatchedSubjects

        If iU = stSubject Then

            oDocument.RemoveFromFolder ("($Inbox)")

        End If

    Next iU



    Set oNextDocument = oView.GetNextDocument(oDocument)

    Set oDocument = oNextDocument

    If oDocument Is Nothing Then

        Set oDocument = oView.getfirstdocument

    End If

Loop