View Full Version : [SOLVED:] Outlook VBA Move / Delete based on item type (Help)
I'm looking for outlook help in writing some vba to look in my deleted items and find the different types of items in there and then differentiate between items I want and items I don't want. With the items I want I would move to a folder called "Trash" and with the items I don't want I would have them deleted permanently.
With that said I suppose my first step is to figure out how to determine all of the items within my deleted folder and decide which ones I want and don't want.
when I run this code
Public Sub GetTypeNamesDeleted()
Dim myOlItems As Outlook.Items
Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderDeletedItems).Items
Dim msg As Object
Dim s As String
Dim n As Integer
n = FreeFile()
Open "C:\Users\U00JBK7\test.txt" For Output As #n
For Each msg In myOlItems
Debug.Print TypeName(msg)
s = TypeName(msg)
Print #n, s
Next msg
Close #n
End Sub
it tells me that I have the following types in my deleted folder:
AppointmentItem
MailItem
MeetingItem
ReportItem
PostItem
SharingItem
DistListItem
TaskItem
ContactItem
Now that I know that how do I know by looking which one's I would want and which ones I don't want. Is there an icon / type matcher somewhere? When I scroll through my deleted items I have no idea what's considered an "AppointmentItem" or a "MeetingItem".
westconn1
08-20-2014, 02:45 PM
I have no idea what's considered an "AppointmentItem" or a "MeetingItem".
while most are easy, for testing, you can figure out which items are which try like
if typename(msg) = "AppointmentItem" then msg.display
try on small collection of items else you might have too many items displayed
you could also add the item subject to your output like
print #n, s, msg.subject
Alright, well here's my code thus far! Thanks for the help! It checks the Deleted Items folder and if it's a mail item it will mark it as read and move it to the Trash folder. If it's not a mailitem then it will delete it. The one thing I can't figure out is why it's stopping at the halfway point. I ran it when I had 1760 and it left me with 880 items in the deleted items folder. I ran it again and ended up with 440, again and then 220. Weird huh? anyone have any ideas?
Public Sub GetTypeNamesDeleteOrMove()
Dim myOlItems As Outlook.Items
Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderDeletedItems).Items
Dim oTarget As Outlook.MAPIFolder
Set oTarget = Application.GetNamespace("MAPI").Folders("MyMailboxName").Folders("Trash")
Dim msg As Object
For Each msg In myOlItems
If TypeName(msg) = "MailItem" Then
msg.Unread = False
msg.Move oTarget
Else
msg.Delete
End If
Next msg
End Sub
Charlize
08-21-2014, 08:00 AM
When you want to delete / move items, you better start from the last one in your collection of items. Also better use a counter instead of a for each loop.
How do I do this? Do you have an example of how to use a counter and how to start from the bottom?
westconn1
08-21-2014, 02:31 PM
try like
For msg = myOlItems.count to 0 step -1 ' work from end to top, else counter is messed up by removing items
If TypeName(myOlItems(msg)) = "MailItem" Then
myOlItems(msg).Unread = False
myOlItems(msg).Move oTarget
Else
myOlItems(msg).Delete
End If
Next msg
Charlize
08-21-2014, 11:27 PM
@westconn1 : wouldn't the 0'th item gave an error since it can't be processed.
For msg = myOlItems.count To 0 Step -1 ?
westconn1
08-22-2014, 02:37 AM
thnx charlize, my bad
should of course be
for msg = myolitems.count to 1 step -1
I get a type mismatch when I run the code on the "msg". Should I Dim it something other than Object?
westconn1
08-27-2014, 02:18 AM
just change the variable name
in this case the variable type should be integer
That's it, I didn't know what type of variable it should be. Works like a charm now. The finished code is below and in case anyone else wants to use it you replaced "MyMailboxName" with the name of your mailbox and I put it in my Microsoft Outlook objects / ThisOutlookSession so that it runs every time I start outlook. This is going to help me a lot, big thanks to westconn1 and Charlize!
Private Sub Application_Startup()
Dim msg As Integer
' Start of moving items from DeletedItems to Trash folder and deleting all items that are not MailItem
Dim myOlItems As Outlook.Items
Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderDeletedItems).Items
Dim oTarget As Outlook.MAPIFolder
Set oTarget = Application.GetNamespace("MAPI").Folders("MyMailboxName").Folders("@Trash")
For msg = myOlItems.Count To 1 Step -1
If TypeName(myOlItems(msg)) = "MailItem" Then
myOlItems(msg).Unread = False
myOlItems(msg).Move oTarget
Else
myOlItems(msg).Delete
End If
Next msg
' Start of moving items from SentItems to Sent folder and deleting all items that are not MailItem or MeetingItem
Dim myOlItemsSent As Outlook.Items
Set myOlItemsSent = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderSentMail).Items
Dim oTargetSent As Outlook.MAPIFolder
Set oTargetSent = Application.GetNamespace("MAPI").Folders("MyMailboxName").Folders("@Sent")
For msg = myOlItemsSent.Count To 1 Step -1
If TypeName(myOlItemsSent(msg)) = "MailItem" Or TypeName(myOlItemsSent(msg)) = "MeetingItem" Then
myOlItemsSent(msg).Unread = False
myOlItemsSent(msg).Move oTargetSent
Else
myOlItemsSent(msg).Delete
End If
Next msg
End Sub
Marzio
11-11-2014, 01:35 AM
Hi,
I try to test your code in OUTLOOK 2013, but the statement:
Dim myOlItems As Outlook.Items
Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderDeletedItems).Items
Return Runtime error
Regards
Marzio
westconn1
11-21-2014, 12:41 AM
you should have started a new thread for this question
as i do not have outlook 2013 installed, hard to test your code, maybe someone else can, or i might try later on a different machine
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.