问题
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