如何将所有电子邮件移动到除最近的文件夹之外的其他文件夹

时间:2012-12-04 16:27:11

标签: vba outlook outlook-2010 outlook-vba

我想将除最新电子邮件之外的所有电子邮件从一个文件夹移动到另一个文件夹。下面的代码移动除了一封电子邮件之外的所有邮件但不是最后收到的电关于如何修改它的任何建议?我是否必须参考.ReceivedTime给它一个时间参考?提前谢谢!

For i = objstartFolder.Items.Count - 1 To 0 Step -1

    With objstartFolder.Items(2)

        On Error Resume Next

                .Move objMoveFolder

   End With

顺便说一句,我也试过了

 For i = objstartFolder.Items.Count To 1 Step -1
 For i = 1 To objstartFolder.Items.Count Step 1
 For i = 0 To objstartFolder.Items.Count -1 Step 1

2 个答案:

答案 0 :(得分:0)

先尝试排序,然后删除。

objstartFolder.Items.Sort“已收到”,错误

有关示例,请参阅http://msdn.microsoft.com/en-us/library/aa163981(v=office.10).aspx

编辑2015 02 16

我最初直接对文件夹中的项目进行了排序。 如果它有效,它将是不可靠的,因为真/假是相同的

Sub SortByReceived_Move()

    Dim myNameSpace As Namespace
    Dim myFolder As folder
    Dim myMoveToFolder As folder
    Dim myItems As Items
    Dim j As Long

    Set myNameSpace = Application.GetNamespace("MAPI")
    Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox).Folders("@Play")
    Set myMoveToFolder = myFolder.Folders("@Test")

    Set myItems = myFolder.Items

    myItems.sort "Received", True
    Debug.Print myItems(1).Subject & " - " & myItems(1).ReceivedTime

    For j = myItems.count To 2 Step -1
        Debug.Print myItems(j).Subject & " - " & myItems(j).ReceivedTime
        myItems(j).Move myMoveToFolder
    Next j

ExitRoutine:
    Set myNameSpace = Nothing
    Set myFolder = Nothing
    Set myMoveToFolder = Nothing
    Set myItems = Nothing

End Sub

编辑2015 02 16结束

答案 1 :(得分:0)

首先,不要使用多点符号,尤其是在循环中 - 每个“。”将返回一个全新的COM对象。在对Items集合进行排序和循环之前缓存它。您可能还想先限制集合。


Items = objstartFolder.Items;
Items.Restrict("[ReceivedTime] >= '2013-01-01' ");
Items.Sort("ReceivedTime");
for (int i=Items.Count; i >= 1; i--)<
{
    Item = Items.Item(i);
    ...
    Item.Move(SomeOtherfolder); 
}