z1lent
z1lent

Reputation: 147

Outlook VBA Incorrectly Replying to Emails in Folder

As referenced from this post, Excel VBA for searching in mails of Outlook, I changed the code slightly to resend all emails to the same recipient in the "sent items" folder.

Function ReplyEmail()

    Dim olMail
    Dim olMails

    Set olApp = CreateObject("Outlook.Application")

    Set olNs = olApp.GetNamespace("MAPI")
    Set olFldr = olNs.GetDefaultFolder(olFolderSentMail)
    Set olMails = olFldr.Items

    For Each olMail In olMails

        olMail.ReplyAll
        olMail.Importance = 2
        olMail.Subject = "RE: 2ND " & olMail.Subject
        olMail.Send

    Next olMail

End Function

However, for some strange reason, this function sends only half of all emails in designated folder. If there are 9 emails, the function sends out 5 (4 left), then if I run another iteration, the function sends out 2, and so on...

If I change olMail.Send to olMail.Display, then the function displays all emails in pop-up frames.

Has any one encountered this issue or know what's the reason behind it?

Thanks.

Upvotes: 0

Views: 62

Answers (1)

Eric Legault
Eric Legault

Reputation: 5834

For starters, I don't understand why you are not using the MailItem object returned from the ReplyAll method to set the properties and call .Send on that object and not the copy of the sent item represented by your olMail variable.

Otherwise I suspect the collection of items is changing. Change the For Each to a For with a reverse counter loop and the weirdness should go away.

Upvotes: 2

Related Questions