PDA

View Full Version : Solved: Outlook Macro - Move All 'Deleted Items' to Personal Folder - Almost Works!



morganm
11-05-2008, 03:27 PM
Almost got it working but something eludes me. I can get it working if I just make it move active selected items but that's not what I want it to do. Basically I want it to just sweep all items from my exchange Inbox - Deleted Items into 'Deleted Items Archive' - Deleted Items folder which is a personal folder.

Here's what I have thus far and it runs with no explicit errors however it doesn't do anything. I know it's not very clean and I'm quite new to VB :)

Please advise

Sub MoveDeletedItems()
' Move selected messages to "Deleted Items" folder in "Deleted Items Archive" personal folder.
On Error Resume Next
'Dim and Set
Dim objFolder As Outlook.MAPIFolder
Set objFolder = Outlook.Application.GetNamespace("MAPI").Folders("Deleted Items Archive").Folders("Deleted Items")
Dim objTrash As Outlook.MAPIFolder
Set objTrash = Outlook.Application.GetNamespace("MAPI").Folders(olFolderDeletedItems)
Dim objItem As Object
'End Dim and Set
'Move Items
For Each objItem In objTrash
If objFolder.DefaultItemType = olMailItem Then
If objItem.Class = olMail Then
objItem.Move objFolder
End If
End If
Next
'End Move Items
'Cleanup
Set objItem = Nothing
Set objFolder = Nothing
Set objInbox = Nothing
Set objNS = Nothing
'End Cleanup
End Sub

jfournier
11-06-2008, 09:16 AM
You should be setting objTrash to Outlook.Application.GetNamespace("MAPI").Folders(olFolderDeletedItems).items , which is the collection of items in your deleted items folder.

To get a reference to the deleted folders items I would actually use:

Outlook.Application.Session.GetDefaultFolder(olFolderDeletedItems).items

but either way should work...

morganm
11-06-2008, 11:02 AM
Thank you for the clarification on setting objTrash. I have updated my macro to include that.

However it still does not function as intended :(

jfournier
11-06-2008, 11:09 AM
I would take out the "If objFolder.DefaultItemType = olMailItem Then " line and corresponding End If line. Checking if the type of objItem is a mailitem should be enough.

If that doesn't help, you should step through the code to see why it's not completing...

morganm
11-06-2008, 03:44 PM
I get a "Type Missmatch" for this line:

Set objTrash = Outlook.Application.Session.GetDefaultFolder(olFolderDeletedItems).Items

morganm
11-06-2008, 03:52 PM
OK so I changed
Dim objTrash As Outlook.MAPIFolder
to
Dim objTrash As Outlook.Items
No more Type Missmatch error and it works!

BUT

Only for emails. There's some other things lingering in there like old meeting requests and calendar items. Ideas for broadening the scope of this macro to include ANYTHING in the Deleted Items folder?

Thanks for the help!

ps: Here's the script thus far

Sub MoveDeletedItems()
'Move messages from Exchange mailbox folder "Deleted Items" to "Deleted Items Archive" personal folder's "Deleted Items" folder.
'Dim and Set
Dim objFolder As Outlook.MAPIFolder
Set objFolder = Outlook.Application.GetNamespace("MAPI").Folders("Deleted Items Archive").Folders("Deleted Items")
Dim objTrash As Outlook.Items
Set objTrash = Outlook.Application.Session.GetDefaultFolder(olFolderDeletedItems).Items
Dim objItem As Object
'End Dim and Set
'Move Items
For Each objItem In objTrash
If objItem.Class = olMail Then
objItem.Move objFolder
End If
Next
'End Move Items
'Cleanup
Set objItem = Nothing
Set objFolder = Nothing
Set objInbox = Nothing
Set objNS = Nothing
'End Cleanup
End Sub

dewey1973
11-07-2008, 09:55 AM
I'm very excited to see your macro. I tried writing a similar macro and while it mostly works, it has problems as well. I'll be scouring your code to see what I can learn.

The thread about my macro is here (I can't post links yet so you will have to make some substitutions:
www[dot]vbaexpress[dot]com/forum/showthread.php?t=22342

jfournier
11-07-2008, 09:59 AM
Your code is pretty much done as far as moving any item in the folder...You can probably just remove the If objItem.Class = olMail Then condition and anything in the Deleted Items folder will get moved...I think anything that can end up in that folder has the "move" method on it, so you should be all set, like I said, if you remove the If objItem.Class = olMail condition...

morganm
11-07-2008, 02:04 PM
Yep, that did it and I see why now that you mentioned it. That IF THEN condition was what was limiting it to only olMail objects. Now it just moves everything from Deleted Items to objTrash!

Thanks for the help jfournier :bow: