VBA to move read emails to specific folders

柔情痞子 提交于 2019-12-25 18:36:52

问题


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


回答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:

  • How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)
  • How To: Use Restrict method to retrieve Outlook mail items from a folder

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.



来源:https://stackoverflow.com/questions/43633958/vba-to-move-read-emails-to-specific-folders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!