I have some code that grabs a set of emails from a folder like such:

[vba] Set OutApp = CreateObject("Outlook.Application")
Set OutNS = OutApp.GetNameSpace("MAPI")
Set OutRecipient = OutNS.CreateRecipient(ABC_MailboxName)
Set OutFolder = OutNS.GetSharedDefaultFolder(OutRecipient, 6) 'olFolderInbox = 6

Set OutItems = OutFolder.Items[/vba]
I then iterate through the messages: if a MailItem has an attachment with the correct name, the attachment is saved to a temp folder.


My question is: is my set of MailItems static or dynamic?

In other words, what happens if a new email is sent to this folder (separately, either by an open instance of Outlook or the Exchange server)? Is my set of items updated to include the new email, or is it static from the point when I put the Object handle on the Folder.Items?



Second question: Deleting those emails

Once I've saved all the attachments, then I have a loop to go back and delete all of the emails. However, this code doesn't work! It always seems to leave ONE email in the box, which suggests that I'm missing something about how the MailItem collections work.

[vba]
For Each OutMail In OutItems

Set OutAttachments = OutMail.Attachments

For Each OutAttach In OutAttachments

If OutAttach.Filename = constABCInputFName Then
OutMail.Delete
End If

Next OutAttach

Next OutMail[/vba]