Highlight And Move Multiple Emails


May 30, 2008 – 12:58 pm by JP

Okay so I said I would be writing some VBA code to export contacts and emails from Outlook to Excel, well, I lied, sort of. That code is still coming, but before I post it, here is a routine that lets you run VBA code on multiple messages at the same time. Most of my routines so far have been written to run on one email at a time:

Resend This Message
Save Incoming Attachments
Read those image files in Outlook

and so on.

This macro is perfect for manual execution of a routine on multiple emails. In the example below, I select a few emails from my Inbox and then move them to another folder. You could easily adapt this macro to forward all the selected messages, save attachments from all of them, etc.

VBA:
  1. Sub MoveToFolder()
  2.  
  3. Dim olMyFldr As Outlook.MAPIFolder
  4. Dim MsgColl As Object
  5. Dim Msg As Outlook.MailItem
  6. Dim objNS As Outlook.NameSpace
  7. Dim i As Long
  8.  
  9. ' check if we have multiple items selected
  10. On Error Resume Next
  11. Select Case TypeName(Application.ActiveWindow)
  12.     Case "Explorer"
  13.         ' a collection of selected items
  14.         Set MsgColl = ActiveExplorer.Selection
  15.     Case "Inspector"
  16.         ' only one item was selected
  17.         Set Msg = ActiveInspector.CurrentItem
  18. End Select
  19. On Error GoTo 0
  20.  
  21. If (MsgColl Is Nothing) And (Msg Is Nothing) Then
  22.     GoTo ExitProc
  23. End If
  24.  
  25. Set objNS = Outlook.GetNamespace("MAPI")
  26. Set olMyFldr = objNS.GetDefaultFolder(olFolderInbox).Folders(”Completed”)
  27.  
  28. ' now we can act on the msg collection,
  29. ' or on the individual msg we selected
  30.  
  31. If Not MsgColl Is Nothing Then
  32. ' we selected multiple items
  33.     For i = 1 To MsgColl.Count
  34.        ' set an obj reference to each mail item so we can move it
  35.         Set Msg = MsgColl.Item(i)
  36.             With Msg
  37.                 .UnRead = False
  38.                 .Move olMyFldr
  39.             End With
  40.     Next i
  41. ElseIf Not Msg Is Nothing Then
  42.     With Msg
  43.         .UnRead = False
  44.         .Move olMyFldr
  45.     End With
  46. End If
  47.  
  48. ExitProc:
  49. Set Msg = Nothing
  50. Set MsgColl = Nothing
  51. Set olMyFldr = Nothing
  52. Set objNS = Nothing
  53. End Sub

Rows 36-39 and 43-44 are the sections you would replace with your own code. The first
part (right after "Set Msg = MsgColl.Item(i)") is the one that loops through each
selected message and runs your code. So you can move, forward, copy, export, reply
with attachments, etc, each mail item you selected. The second one runs only if one
mail item is selected.

In both cases, the object variable "Msg" is a reference to the current mail item.
That's what you will be manipulating.

If you're saving attachments, for extra credit try adding the SelectFolder() Function
from the Save Incoming Attachments post.

Enjoy,
JP


If you enjoyed this page:
  • StumbleUpon
  • Technorati
  • Digg
  • Google
  • del.icio.us
  • MisterWong

Print This Post Print This Post  |  Email This Post Email This Post  |  Permalink  |  Subscribe to this feed Subscribe now!

Filed Under: Outlook, VBA
Tags: , , ,

This post has 534 views since May 30, 2008 – 12:58 pm.

Post a Comment

To post VBA code in your comment, use [VBA] tags, like this: [VBA]Code goes here[/VBA].





Subscribe without commenting

Keep Reading:

Browse Posts:


« Open Any Email Attachment From Outlook || Moving to Wordpress »