Udostępnij za pośrednictwem


Outlook 2010: Macro for Moving Selected Items

It has been a really long time since I wrote Microsoft Outlook VBA code. But when a customer last week asked me if there was a way to write some VBA code to move selected Inbox items to an Archive folder, I felt right at home. Here's what I came up with for him:

 Sub ArchiveItems()

    ' Moves each of the selected items on the screen to an Archive folder.
    Dim olApp As New Outlook.Application
    Dim olExp As Outlook.Explorer
    Dim olSel As Outlook.Selection
    Dim olNameSpace As Outlook.NameSpace
    Dim olArchive As Outlook.Folder
    Dim intItem As Integer

    Set olExp = olApp.ActiveExplorer
    Set olSel = olExp.Selection
    Set olNameSpace = olApp.GetNamespace("MAPI")
    ' This assumes that you have an Inbox subfolder named Archive.
    Set olArchive = olNameSpace.GetDefaultFolder(olFolderInbox).Folders("Archive")

    For intItem = 1 To olSel.Count
        olSel.Item(intItem).Move olArchive
    Next intItem

End Sub

Comments

  • Anonymous
    September 23, 2010
    Hey there, thanks for posting this, I think it's going to be a big help for me. I'm trying to move things to a top level folder called [Archived], do you know how I would specify that?

  • Anonymous
    October 25, 2010
    The comment has been removed

  • Anonymous
    October 25, 2010
    The comment has been removed

  • Anonymous
    February 09, 2011
    Thanks for this, gave me the code I needed to solve a problem that I'd hit a road block on!

  • Anonymous
    July 26, 2011
    Thanks!  Had custom ALT+<n> keyboard shortcuts implemented in 2007, but only recently realize (post upgrade) that i couldn't use the same method.  Once I realized the Quick Access bar hijacked the Alt+<n> combos, I found your macro, and all is zen again. ;)

  • Anonymous
    September 01, 2014
    THANKYOU!!!!!!!!!! I just spent 6 hours searching, trying and debugging example code from every site on the internet Every time I got object not defined errors Your code worked first time :)))) As far as I can tell you are the only person in the world who has ever posted a working macro of how to move an email in Outlook - congratulations and thanks again

  • Anonymous
    November 05, 2014
    You saved me a lot of time ! Can you address how to move an item to a level 2 sub folder ? I have my Inbox, which is divided into department folders (Inbox2145 Project Management). My "2145 Project Management"-folder I have divided into initials-folders, i.e.

  • Inbox2145 Project ManagementADLB
  • Inbox2145 Project ManagementFTNS
  • Inbox2145 Project ManagementJLKN
  • Inbox2145 Project ManagementPHDN How do I address this into the perfect macro code you have written above ?
  • Anonymous
    November 05, 2014
    I just added .Folders("ADLB"), i.e. .... Set olArchive = olNameSpace.GetDefaultFolder(olFolderInbox).Folders("2145 Project Management").Folders("JBJA") ... wauv, that was easy ;-)