Consulting

Results 1 to 8 of 8

Thread: Macro to identify deleted items w/IMAP and Outlook

  1. #1
    VBAX Newbie
    Joined
    Feb 2008
    Posts
    2
    Location

    Macro to identify deleted items w/IMAP and Outlook

    Hello,

    I am trying to find a way to identify deleted items in outlook using IMAP. As you know when using IMAP and outlook when you delete a mail item, it puts a line through the item signifying that it is deleted, then you purge the folder to permanently delete all deleted items from the server and hence removes them from your Inbox. What I want to do is run a maro that identifies all deleted items, moves them to a different folder (ie. a "deleted items" folder) and then purge the current folder. Essentially this will give a deleted items folder, with a clean Inbox.

    I can take care of moving and purging the emails, what I can't figure out is how to identify what messages are marked for deletion????

    I either need a way to determine if an item is marked for deletion or a way to determine when the delete button has been pressed so I can run code at that time.

    Can anyone help with this? It's driving me nuts!!

  2. #2
    VBAX Newbie
    Joined
    Feb 2008
    Posts
    2
    Location

    Solution

    Ok, so I finally figured this out. I would have assumed this would be a common bit of code, but I guess not. Using a reference to Redemption.dll I was able to access extended properties to find out if an IMAP message is marked for deletion. Here is the code if anyone is interested. There may be better ways to do some of this stuff, but I am new to outlook vba and this works.

    'Macro loops through all mail items in current folder and if marked for deletion
    'moves them to a folder called 'Deleted Items' then purges the current folder
    'this allows a an IMAP account in outlook have the appearance of a Deleted Items folder.
    'there is not much error checking, etc, but you get the jist.

    Sub go()
        Const pscImapDeleted As String = "{00062008-0000-0000-C000-000000000046}"
        Const piDelId As Integer = &H8570
        Const plDelTag As Long = 0
        Dim mInBoxItems As Outlook.Items
        Dim pobjSafeMailItem As Object
        Dim pvDelId As Variant
        Dim pvDeletedTag As Variant
        Dim utils As Object
        Dim iMailLoop As Integer
        Set myOlApp = CreateObject("Outlook.Application")
        Dim myExplorer As Explorer
        Set myExplorer = myOlApp.ActiveExplorer
        Set myNameSpace = myOlApp.GetNamespace("MAPI")
        ' test for mail folder
        folderType = myExplorer.CurrentFolder.DefaultItemType
        ' is it a mail folder?
        If TypeName(myExplorer) = "Nothing" Or folderType <> 0 Then
            GoTo badMailbox
        End If
        ' get the current folder
        Set thisfolder = myExplorer.CurrentFolder
        Set mInBoxItems = thisfolder.Items
        Set pobjSafeMailItem = CreateObject("Redemption.SafeMailItem")
        For iMailLoop = 1 To mInBoxItems.Count
             Set mobjolmailobject = mInBoxItems.Item(iMailLoop)
             Set utils = CreateObject("Redemption.MAPIUtils")
             pobjSafeMailItem.Item = mobjolmailobject
             pvDelId = pobjSafeMailItem.GetIDsFromNames(pscImapDeleted, piDelId)
             pvDelId = pvDelId Or &H3
             pvDeletedTag = pobjSafeMailItem.Fields(pvDelId)
             If pvDeletedTag Then
                 Set myItem = mInBoxItems.Item(iMailLoop)
                 Set TrashFolder = thisfolder.Folders("Deleted Items")
                 myItem.Move (TrashFolder)
             End If
        Next
        'purge deleted items from folder.
        Dim myBar As CommandBar
        Set myBar = Application.ActiveExplorer.CommandBars("Menu Bar")
        Dim myButtonPopup As CommandBarPopup
        Set myButtonPopup = myBar.Controls("Edit")
        Dim myButton As CommandBarButton
        Set myButton = myButtonPopup.Controls("Purge Deleted Messages")
        myButton.Execute
        Exit Sub
        badMailbox:
        MsgBox ("This macro is designed to only work on mail folders.")
        Exit Sub
    End Sub
    Last edited by Aussiebear; 12-29-2024 at 07:08 PM.

  3. #3
    There is a way to get the "Marked For Deletion" (aka "IMAP Status") flag without Redemption using an Outlook Property Accessor. The Property Id is ""http{colon}//" & "schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/85700003". It returns a Long/Boolean 0 = Not Marked, 1 = Marked.

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,282
    Location
    Maybe try this method?

    Sub CheckIfItemIsMarkedForDeletion()
        Dim objItem As Object
        ' Get the current item (e.g., email, appointment)
        Set objItem = Application.ActiveInspector.CurrentItem
        ' Check if the item is marked for deletion
        If objItem.DeleteOnSubmit Then
            ' Item is marked for deletion
            MsgBox "This item is marked for deletion."
        Else
            ' Item is not marked for deletion
            MsgBox "This item is not marked for deletion."
        End If
    End Sub
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,391
    Location
    This thread was last active in 2008. I hardly think the OP is still looking for a solution...
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  6. #6
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,282
    Location
    So no one else might be looking for a solution? A little more constructive comments please Paul.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  7. #7
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,391
    Location
    Well, if they are, they haven't been posting here...
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  8. #8
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,282
    Location
    Just because they might not be posting does not mean that a possible solution can't be shown.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

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