0

I'm struggling to find a definitive answer to what must be a reasonably common problem?

My boss has folders in their inbox for members of the team, but doesn't want to move the emails there until they have been read out of the inbox. So I am trying to write a macro which follows these logical steps:

For each mailitem in inbox:
    Check if read
      Check sender name
      select case True
        sender name like existing folder name
        move item to corresponding folder name
next mailitem

Can you help me put this together, I'm rather lost at sea on this one!!

Phil

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
Philip Day
  • 69
  • 7
  • 5
    Welcome to StackOverflow. Please note, that this is not a free code-writing service. Yet, we are eager to help fellow programmers (and aspirants) writing their own code. Please read the help topics on [How do I Ask a Good Question](http://stackoverflow.com/help/how-to-ask). Afterwards, please update your question with the VBA code you have written thus far in order to complete the task(s) you wish to achieve. We will be here waiting for you. Ready to assist and help you finalize *your code*. –  Apr 26 '17 at 12:19
  • I formatted your code, but am not 100% sure I got the indentation right to reflect your meaning. Please review and edit if necessary. – Jim Mischel Apr 26 '17 at 13:51
  • Putting in pseudo-code does not change this from being a code-writing service-type question. Someone has answered your question, although I don't think they really should have as it won't teach you anything. If your boss is asking you to do this exercise, they probably want you to understand and learn VBA, not to get someone else to write it. I hope you send some of your salary to Eugene as he just did your job for you, although you failed to learn the knowledge that will help you further your career....(continued) – OpiesDad Apr 26 '17 at 18:37
  • If you had done a google search for "how to get mail in inbox vba stack overflow", you would have found, http://stackoverflow.com/questions/24321752/outlook-vba-how-to-loop-through-inbox-and-list-from-email-email-address-if-subje among many other good questions which would have started you off and you could have continued googling things until you figured it out or had a problem you couldn't figure out. You are, of course, welcome here, but try a little effort first. – OpiesDad Apr 26 '17 at 18:40

1 Answers1

1

Iterating through all items in the Inbox folder is not a good idea. You need to use the Find/FindNext or Restrict methods of the Items class to find all items that correspond to your conditions (read and sender name). Read more about these methods in the following articles:

Then you can use the Move method of the MailItem class to move a Microsoft Outlook item to a new folder. For example:

Sub MoveItems() 
 Dim myNameSpace As Outlook.NameSpace 
 Dim myInbox As Outlook.Folder 
 Dim myDestFolder As Outlook.Folder 
 Dim myItems As Outlook.Items 
 Dim myItem As Object 

 Set myNameSpace = Application.GetNamespace("MAPI") 
 Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox) 
 Set myItems = myInbox.Items 
 Set myDestFolder = myInbox.Folders("Personal Mail") 
 Set myItem = myItems.Find("[SenderName] = 'Eugene Astafiev'") 
 While TypeName(myItem) <> "Nothing" 
  myItem.Move myDestFolder 
  Set myItem = myItems.FindNext 
 Wend 
End Sub

You may find the Getting Started with VBA in Outlook 2010 article helpful.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Thank you, this looks great. I have done some VBA before and feel confident to take this forward when i get back to work! I'm not sure how to mark this as the answer, but clearly Eugene has indeed come up trumps. I'm sorry that I got it wrong, I didn't want to put some utter rubbish, but that of course doesn't excuse a failure to follow the protocol. – Philip Day Apr 27 '17 at 17:40