Search sent mail and move multiple mail to a folder

风流意气都作罢 提交于 2019-12-11 07:43:29

问题


I try to search the SentMail folder for mail where To address ends with, lets say .cn and move it to a personal folder.

The code work but doesn't move all the mail, for every time I run it it's take a few more mail.

If I have 24 mails that ends with .cn it takes 13,6,3,2 so in the end it finds all the mail but not on the same run.

Anyone have any ideas?

Sub Search_SentMail()

Dim myOlApp As New Outlook.Application
Dim MyNameSpace As Outlook.NameSpace
Dim MyInbox As Outlook.MAPIFolder
Dim myitems As Outlook.Items
Dim myItem As Object

Set MyNameSpace = myOlApp.GetNamespace("MAPI")
Set MyInbox = MyNameSpace.GetDefaultFolder(olFolderInbox)
Set myOutbox = MyNameSpace.GetDefaultFolder(olFolderSentMail)
Set myitems = myOutbox.Items

Dim junk As Outlook.Folder
Set junk = MyNameSpace.GetDefaultFolder(olFolderInbox)
Set junk = junk.Folders("CN")
Count = 0
For Each myItem In myitems
If TypeOf myItem Is Outlook.MailItem Then
If Right(myItem.To, 4) = ".cn'" Then
myItem.Move junk
Count = Count + 1
End If
End If
Next myItem

Debug.Print Count
Set MyNameSpace = Nothing
Set MyInbox = Nothing
Set myOutbox = Nothing
Set myitems = Nothing
Set junk = Nothing
End Sub

回答1:


The answer was here: For Each loop: Some items get skipped when looping through Outlook mailbox to delete items

Thanks @niton



来源:https://stackoverflow.com/questions/52273623/search-sent-mail-and-move-multiple-mail-to-a-folder

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