Consulting

Results 1 to 9 of 9

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

  1. #1

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

    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

    [VBA]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[/VBA]

  2. #2
    You should be setting objTrash to Outlook.Application.GetNamespace("MAPI").Folders(olFolderDeletedItems).item s , 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...

  3. #3
    Thank you for the clarification on setting objTrash. I have updated my macro to include that.

    However it still does not function as intended

  4. #4
    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...

  5. #5
    I get a "Type Missmatch" for this line:

    [VBA]Set objTrash = Outlook.Application.Session.GetDefaultFolder(olFolderDeletedItems).Items[/VBA]

  6. #6
    OK so I changed
    [vba]Dim objTrash As Outlook.MAPIFolder [/vba]
    to
    [vba]Dim objTrash As Outlook.Items[/vba]
    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

    [VBA]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[/VBA]

  7. #7
    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

  8. #8
    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...

  9. #9
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •