Search code examples
vbaoutlookoutlook-2007

How can I create a script to move the currently active email in the Inbox to another folder in Outlook 2007


I sometimes get emails that I want to keep but to move them into the appropriate folder can be a pain. How can I execute a script that will move (like using C-S-v) the email I'm looking at into a certain folder called "buffer", for instance?

I'm using Outlook 2007.

thanks.


EDIT: there isn't any criteria that can be created to automate this process like through a rule. it is merely a judgment call I make as i'm staring at it.


Solution

  • Here's the code I'm using.

    Sub MoveSelectedMessagesToFolder()
    'Originally written by Chewy Chong
    'Taken from http://verychewy.com/archive/2006/04/12/outlook-macro-to-move-an-email-to-folder.aspx
    'Thanks Chewy!
    'Ken
    On Error Resume Next
        Dim objFolder As Outlook.MAPIFolder, objInbox As Outlook.MAPIFolder
        Dim objNS As Outlook.NameSpace, objItem As Outlook.MailItem
    
        Set objNS = Application.GetNamespace("MAPI")
        Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
        'For the "Item" portion, I used the name of the folder exactly as it appear in the ToolTip when I hover over it.
        Set objFolder = objNS.Folders.Item("Personal Folders").Folders.Item("Buffer")
    
    'Assume this is a mail folder
    
        If objFolder Is Nothing Then
            MsgBox "This folder doesn't exist!", vbOKOnly + vbExclamation, "INVALID FOLDER"
        End If
    
        If Application.ActiveExplorer.Selection.Count = 0 Then
            'Require that this procedure be called only when a message is selected
            Exit Sub
        End If
        For Each objItem In Application.ActiveExplorer.Selection
            If objFolder.DefaultItemType = olMailItem Then
                If objItem.Class = olMail Then
                    objItem.Move objFolder
                End If
            End If
        Next