Consulting

Results 1 to 13 of 13

Thread: Outlook VBA Move / Delete based on item type (Help)

  1. #1
    VBAX Regular
    Joined
    May 2008
    Posts
    11
    Location

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

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

  3. #3
    VBAX Regular
    Joined
    May 2008
    Posts
    11
    Location
    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

  4. #4
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    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.

  5. #5
    VBAX Regular
    Joined
    May 2008
    Posts
    11
    Location
    How do I do this? Do you have an example of how to use a counter and how to start from the bottom?

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

  7. #7
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    @westconn1 : wouldn't the 0'th item gave an error since it can't be processed.

    For msg  = myOlItems.count To 0 Step -1
    ?

  8. #8
    thnx charlize, my bad
    should of course be
    for msg = myolitems.count to 1 step -1

  9. #9
    VBAX Regular
    Joined
    May 2008
    Posts
    11
    Location
    I get a type mismatch when I run the code on the "msg". Should I Dim it something other than Object?

  10. #10
    just change the variable name
    in this case the variable type should be integer

  11. #11
    VBAX Regular
    Joined
    May 2008
    Posts
    11
    Location
    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

  12. #12
    VBAX Newbie
    Joined
    Nov 2014
    Posts
    5
    Location
    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

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

Tags for this Thread

Posting Permissions

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