2

我想将所有电子邮件(最近的电子邮件除外)从一个文件夹移动到另一个文件夹。

下面的代码移动了除一封电子邮件之外的所有电子邮件,但它不是收到的最后一封电子邮件。

我是否必须参考 .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
4

2 回答 2

0

尝试先排序然后删除。

objstartFolder.Items.Sort “收到”,假

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

编辑 2015 02 16

本来我是直接把文件夹里的项目排序的。如果它有效,那将是不可靠的,因为 True / False 是相同的

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 结束

于 2013-02-09T22:32:44.680 回答
0

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


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); 
}
于 2013-02-10T16:47:10.577 回答