Discussion Locked

Delete outlook messages from a folder - How to

0 helpful
1 reply
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I'm using the following code but it does not delete all the message in a particular folder.
Does anyone know how to correct this code?

For Each MailItem In MyFolder.Items
Debug.Print MailItem.Delete
Next MailItem

Your guidance will be helpfull

Luis

1 reply

by mbranscum

We just had the same issue. We had mission critical emails getting sent to the junk folder. Luckily each call contained "Incoming New Call" in the subject which we could search for in our code. We needed to copy these to the inbox. However, we hit the same snag trying to delete them from the junk folder. Here is the resolution that worked for us.

dim m_NameSpace As Outlook.NameSpace
m_NameSpace = Me.Application.Session
dim m_JunkFolder As Outlook.MAPIFolder
m_JunkFolder = m_NameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderJunk)
dim m_JunkItems As Outlook.Items
m_JunkItems = m_JunkFolder.Items
Dim tempMailItems As New List(Of Object)
Dim tempMailItem As Outlook.MailItem
For Each junkItem As Object In m_JunkFolder.Items
If (TypeOf junkItem Is Outlook.MailItem) Then
m_CurrentJunkMailItem = CType(junkItem, Outlook.MailItem)
If (LCase(m_CurrentJunkMailItem.Subject).Contains("incoming new call")) Then
tempMailItems.Add(junkItem)
End If
End If
Next
For Each junkItem As Object In tempMailItems
tempMailItem = junkItem
junkItem = 0
CType(tempMailItem, Outlook.MailItem).Move(m_Inbox)
Next
tempMailItems.Clear()
tempMailItems = Nothing

This discussion is closed to new replies.