GMAIL IMAP and Trashing Messages Using VBA and Outlook

by Aaron 06/21/08

One of the email options I offer to my clients is the ability to use what Google calls Google Apps which is a business version of their Gmail, Calendaring, and other various services.  The GMAIL service offers the IMAP protocol to synchronize email with various mail clients (Outlook, IPhone, Web...).  The benefit of IMAP is that you will have same version of your mail wherever you access it from.

One of the biggest hang-ups people have with using this is the whole paradigm of archiving messages vs. deleting messages.  Using the IMAP interface the delete command is interpreted as Archive to delete a message you have to move that message into your [GMAIL]/trash (where it will be automatically deleted after 30 days).

One of my bosses at my day job kept pestering me about the ability to delete various marketing email that she had no desire to archive and felt that moving the messages to trash was too much of a hassle.  After trying to convince her that it wasn't too much of a hassle I realized that it was in fact a pain in the butt and there had to be a better way.  So with my expert Google skills I set out to find a solution and I did. outlines 2 specific Outlook idioms, one being the IMAP move to trash issue discussed above.  The code presented there is not specific to GMAIL, so I took his code and made some minor changes to get it to work with a standard Outlook Gmail Setup.  I have tested with multiple account in Outlook 2007 and appears to work as expected. 

To implement the what I'm calling the "Trash" feature follow these instructions (USE AT YOUR OWN RISK)

  1. Open up Outlook
  2. In the menu bar go to tools->Macros->Visual Basic Editor (or hit alt+F11)
  3. Locate ThisOutlookSession Macro and open it
  4. Copy the code below into the Code Area
  5. Save and Close the VBA Window
  6. Create a Toolbar item by going to the menu bar View->Toolbars->Customize
  7. Locate Macros in the Category (left pane)
  8. And Drag the Project1.ThisOutlookSession.TrashMessages up to your toolbar (I put mine next to the X - delete command).
  9. You can give the Trash toolbar item a more friendly name an icon and even assign a hotkey by going back to the Customize Command window  and then Rearrange Commands. Use this article on Windows IT Pro Adding Hotkeys and Toolbar Buttons if you need more help with this.
  10. That's it I hope you find this useful!

Below is the VBA s code:

Sub TrashMessages()

    Set myOlApp = CreateObject("Outlook.Application")
    Dim myNameSpace As NameSpace
    Set myNameSpace = myOlApp.GetNamespace("MAPI")
    Dim myExplorer As Explorer
    Set myExplorer = myOlApp.ActiveExplorer
    'Get the folder type, expected type is 0 i.e. mail folder. If other type of folder
    'being used then abort macro as it should only be used with mail folders.
    folderType = myExplorer.CurrentFolder.DefaultItemType
    'Check that folder is mail folder
    If TypeName(myExplorer) = "Nothing" Or folderType <> 0 Then
    GoTo invalidMailbox
    End If
    'Locate root folder for this account
    Set thisFolder = myExplorer.CurrentFolder
    Do Until thisFolder.Parent = myNameSpace
    Set thisFolder = thisFolder.Parent
    Set accountFolder = thisFolder
    'Identify selected messages
    Dim selectedItems As Selection
    Set selectedItems = myExplorer.Selection
    Dim currentMailItem As MailItem
    Dim iterator As Long
    'Move messages to Deleted Items folder
    Set trashFolder = accountFolder.Folders("[GMAIL]")
    Set trashFolder = trashFolder.Folders("Trash")
    Count = selectedItems.Count
    For iterator = Count To 1 Step -1
        On Error Resume Next
        Set currentMailItem = selectedItems.Item(iterator)
        currentMailItem.Move (trashFolder)
    Exit Sub
    MsgBox ("Macro configured only to work with mail folders! ")
    Exit Sub

End Sub



blog comments powered by Disqus