Move Messages to Folders with Outlook VBA

Let’s say your email management approach is to save important messages to a folder other than your Outlook Inbox. You might save messages from outside vendors to a folder named “Vendor Documents” or messages related to corporate policy to a “Policies” folder. Here’s an Outlook VBA macro that helps to file those messages from your Inbox in a single click:

Sub MoveToFolder(folderName)

 mailboxNameString = "Mailbox - Firstname Lastname"

 Dim olApp As New Outlook.Application
 Dim olNameSpace As Outlook.NameSpace
 Dim olCurrExplorer As Outlook.Explorer
 Dim olCurrSelection As Outlook.Selection

 Dim olDestFolder As Outlook.MAPIFolder
 Dim olCurrMailItem As MailItem
 Dim m As Integer

 Set olNameSpace = olApp.GetNamespace("MAPI")
 Set olCurrExplorer = olApp.ActiveExplorer
 Set olCurrSelection = olCurrExplorer.Selection

 Set olDestFolder = olNameSpace.Folders(mailboxNameString).Folders(folderName)

 For m = 1 To  olCurrSelection.Count
    Set olCurrMailItem = olCurrSelection.Item(m)
    Debug.Print "[" & Date & " " & Time & "] moving #" & m & _
                ": folder = " & folderName & _
                "; subject = " & olCurrMailItem.Subject & "..."
    olCurrMailItem.Move olDestFolder
 Next m

End Sub

Some customizations are necessary to make this all work for your email system. First, copy-and-paste the program to the Visual Basic Editor in Outlook (which you can open with Alt-F11). You’ll need to create a new VB Module (use the Insert->Module menu choice) to hold the macro if you don’t already have custom Outlook macros defined.

Second, the text highlighted in the blue bold font needs to be edited for the name of your Exchange mailbox; you can usually find the name of your mailbox on the left side of the Outlook window at the top, it starts with “Mailbox” like in the example above.

Third, you’ll need to add other VBA macros which call the MoveToFolder() macro with the correct folder name passed as  a parameter. For example, we might create two additional macros one each for the Vendor Documents and the Policies folders; the macro for Vendor Documents might be called MTV() and the Policies folder macro MTP():

Sub MTV()
 MoveToFolder ("Vendor Documents")
End Sub

Sub MTP()
 MoveToFolder ("Policies")
End Sub

(Note, the destination folders should already exist in your Outlook folder list.)

The last step is to assign the macros to Outlook toolbar buttons so you can move the selected messages in your Inbox to the appropriate folders with a click. If you’re managing multiple destination folders, and you created multiple macros (one for each folder), you can also create a new toolbar to organize all your MoveToFolder() macros. Here’s a Microsoft Knowledge Base article that describes how to assign Outlook VBA macros to toolbar buttons: How to Assign a Macro to a Toolbar Button.

The MoveToFolder() macro will work on a single selected message, or a group of messages selected in a sequential range (with a Shift-Click), or a non-sequential range (with Ctrl-Clicks). Also, an activity message will be written to the VBA Editor’s Immediate Window (which is opened with Ctrl-G) so you can go-back and view MoveToFolder() activity. A total win for email convenience, give it a try.

Advertisements

Tags: , ,

33 Responses to “Move Messages to Folders with Outlook VBA”

  1. George Says:

    Hiya, great idea, but didn’t work for me. The movetofolder macro didn’t pop up in my list of macros. Should I have adapted the foldername entries?

  2. Jørgen Carling Says:

    This is great, also for Outlook 2010 – trly a time-saver. However, I am struggling to find the right syntax for moving to subfolders. What if “Vendor Documents” contains subfolders for different vendors? How can I then append the subfolder name correctly to the “MoveToFolder (“Vendor Documents”)” line of the code? Your advice on tis would be greatly appreciated!

  3. saieva Says:

    The syntax should be, for example:

    MoveToFolder(“Vendor Documents\Apple”)

    Happy to hear it’s working Ok for you, and thanks for the confirmation for Outlook 2010.

    Sal.

  4. saieva Says:

    George,

    Did you create a new module for the code (ie, the step where you choose the Insert->Module menu choice in the Outlook VBA Editor)?

    Sal.

  5. Jørgen Carling Says:

    Thanks for the subfolder reply! Unfortunately, it doesn’t work. There’s a run-time error ‘-2147221233 (8004010f)’ and the Debugger points to a line in the top syntax:

    Set olDestFolder = olNameSpace.Folders(mailboxNameString).Folders(folderName)

    Does this need to be modified for the subfolder syntax to work in each macro?

    With Outlook 2007 I used a different syntax(http://www.craigmurphy.com/blog/?p=851) that handles subfolders, but which doesn’t work with Outlook 2010… Also, I much prefer yours since the code for each folder Macro is so short in comparison.

  6. Harald Says:

    Jorgen,
    Did you get it to work? I have the same problem, using outlook 2007.

  7. Harald Says:

    Replacing the specific errorline with:

    Set olDestFolder = Application.GetNamespace(“MAPI”).Folders(folderName)

    Solves the issue (at least for Outlook 2007)

  8. saieva Says:

    Thanks for the fix Harald. I don’t have access to an Exchange Server at this point so I can’t test these changes. Based on the change though, I would double-check the value of the mailboxNameString variable; it may be incorrect or have a typo in its value.

    Sal.

  9. Jørgen Carling Says:

    I got the craigmurphy code that I linked to above to work for 2010 too, so I opted for that one. I will try saieva’s code again with Harald’s correction. Thanks!

  10. Dominik Says:

    Hi,
    thanks for this. Is there also a way to build in a trigger that every message that I’m sending is checked in the “from” field and if the right one applies is is moved to a specific folder?
    Thx
    Dom

  11. saieva Says:

    Hello Dom,

    I’m sure it’s possible from VBA but I never looked into it. Outlook has a rule for processing messages after sending. Would the built-in functionality meet your needs?

    Sal.

  12. Dominik Says:

    Hi Saieva,
    thanks for the comment.
    Unfortunately that won’t work cause I’m sending from several inboxes and there is no specific rule I know of to identify an eMail by the person that has send it (I’m using Outlook 2007). Have tried quite a bit around but the only solution I have would be to take a macro that destinguishes which eMail to take from the “from field”. Haypp to be taught otherwise though.
    Dom
    PS: I’d try to work around it myself but my VBA knowledge just doesn’t cover that…

  13. saieva Says:

    I’m sure it’s possible to create such a trigger in VBA, but I don’t have a solution I can send you. The move to folder code posted can be used in a manual fashion after the message is sent. For example, you can open the Sent Items folder, select messages, and run the macro to move to destination folders. However, that approach would not be triggered automatically.

    Sal.

  14. Prakash Says:

    Saiva – Thank you so much for this code, unfortunately this doesnt work for me in 2003 version of outlook.
    Can you please help mw out fix this?

  15. Prakash Says:

    the problem is –
    There’s a run-time error ‘-2147221233 (8004010f)’ and the Debugger points to a line in the top syntax:

    Set olDestFolder = olNameSpace.Folders(mailboxNameString).Folders(folderName)

    Does this need to be modified for the subfolder syntax to work in each macro?

  16. saieva Says:

    Yes, the script needs to be setup for your mailbox. Specifically, this variable needs to be defined in the script:

    mailboxNameString = “Mailbox – Firstname Lastname”

    There are instructions in the blog posting.

    Sal.

  17. Peter Says:

    Thanks for the script. I am using Outlook 2007 and to access subfolder of my Inbox I had to make one change to your code. Hopfully this helps others who had issues doing the same:

    Set olDestFolder = olNameSpace.Folders(“Mailbox – Smith, Joe”).Folders(“Inbox”).Folders(folderName)

  18. saieva Says:

    Great. Thanks for the feedback Peter.

    Regards,

    Sal.

  19. digitalreins Says:

    Thanks, got me close to what I was looking for. I changed it a little to dynamically pick the folders that you move messages from and to.

    My updated code is posted at http://www.digitalreins.com/how-to-move-outlook-messages-with-vba/

    The code here definitely helped me on the road toward what I needed.

  20. saieva Says:

    Great. Thanks for the reference and for also making your solution public.

    Sal.

  21. Someguy. Says:

    Like others I kept having problems with

    Set olDestFolder = olNameSpace.Folders(mailboxNameString).Folders(folderName)

    It seemed like “mailboxNameString” was to fault, but I couldn’t work out the problem. However another site (I forget which) provided a solution which did away with the mailbox name altogether:

    Set olDestFolder = olNameSpace.GetDefaultFolder(olFolderInbox).Folders(folderName)

    On a separate note (and this was slightly covered by another comment), it appears that in a heirarchy of folders you have to specify each one all the way down, eg:

    Set olDestFolder = olNameSpace.GetDefaultFolder(olFolderInbox).Folders(“Dairy”).Folders(“Milk”).Folders(“Semi-skimmed”)

  22. Ronald Says:

    Hi,
    how should I change the code at the top if I wanted to make a copy of an email in another folder, instead of moving the email? I tried changing “move” to “copy”, but that doesn’t work.
    Ronald

  23. saieva Says:

    Hello Ronald,

    I haven’t tested but you can try to change this line in the For loop (right before the Next statement):

    olCurrMailItem.Move olDestFolder

    to

    olCurrMailItem.Copy olDestFolder

    Sal.

  24. Ronald Mook Says:

    Thanks for your quick reply!
    I tried to use copy instead of move, but that doesn’t work. Outlook 2010 gives an error message after reaching this line of code with Copy. It says Object not supported by this method.
    Ronald

  25. Ronald Says:

    Hi,
    thanks for your quick reply! (I also tried to use my phone to reply, but I think I did something wrong).

    I tried to replace “Move” with “Copy”, but I get an error message telling that the object is not supported by the method (Outlook 2010) everytime I pass this line of code.

    However! I found out the email is indeed copied. But the copy is placed in the inbox, not in the destination folder.
    Good enough for me, if I can move the copy to my destination folder as the next step. Working on that now.

    By the way, something that made me hesitating a while (I know just enough VBA to be dangerous…….), is the mailboxnamestring stuff. I kept thingking I needed something like your example (“mailbox – firstname lastname”), but I could only find “persoonlijke mappen” on my screen (I’m Dutch, by the way). I thought you gave some kind of syntax, where I absolutely needed the term “mailbox” and so on.
    It turns out it is less complicated than I thought (it often is). I could just use “persoonlijke mappen”.

    Ronald.

  26. Ronald Says:

    It works!

    This is the code I need (It copies an email with attachments, if there are any, in the inbox and then moves the original email with attachments to the destination folder I made).

    For m = 1 To olCurrSelection.Count
    Set olCurrMailItem = olCurrSelection.Item(m)
    olCurrMailItem.Copy
    olCurrMailItem.Move olDestFolder
    Next m

    (I removed the debug.print part, as it is not needed to make this work)

    Ronald.

  27. saieva Says:

    Ronald,

    I just tested, it can be accomplished in two steps:

    olCurrMailItem.Copy
    olCurrMailItem.Move olDestFolder

    So add the copy before the move. This approach tested Ok for a single mail item, an adjacent (shift-click) selection, and non-adjacent (ctrl-click) selection.

    Sal.

  28. Hashmi Says:

    Can I use this Macro in stand alone Outlook 2007. I don’t have an Exchange Server.What change do I have to make to “mailboxNameString = “Mailbox – Firstname Lastname” I am very new to VBA so apologies if my assumption is not obvious.

  29. Michael Says:

    Hi, i’m getting a runtime error at the following point:
    olCurrMailItem.Move olDestFolder ‘ERROR STOPS HERE

    this is how is set olDestFolder
    Set olDestFolder = olNameSpace.Folders(mailboxNameString).Folders(“01 MERS”).Folders(folderName)

    Any tips on how to fix it?

  30. Michael Huddlestone Says:

    hi sal, the macro is great but when I use it to run against sub folders I get an error at the code:

    olCurrMailItem.Move olDestFolder

    I set the olDestFolder by the below
    Set olDestFolder = olNameSpace.Folders(“Mailbox – Reports”).Folders(“01 Final”).Folders(folderName)

    I end up getting a run time error. Any Advice?

  31. DK Says:

    Hi,

    I want to add 6 button for six folder like

    Sub MTV()
    MoveToFolder (“AELL”)
    End Sub

    Sub MTP()
    MoveToFolder (“Austria”)
    End Sub

    I need 4 more string to move different emails to different folder.

    Pls advise.

  32. How to Move Outlook Messages with VBA - Digital Reins Says:

    […] be the same, especially if you plan on distributing the code to different users. The example at https://blog.saieva.com/2010/03/27/move-messages-to-folders-with-outlook-vba/ helped me on my way. After a little longer the code below is the code I came up […]

  33. abdi Says:

    Is there a VBA macro for finding “replied emails” to a given folder and move them to another folder?

    Rules setting doesn’t have the function for finding “replied emails”

    Thank you!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: