按VBA

时间:2018-06-06 19:45:05

标签: outlook outlook-vba

我正在尝试按用户定义的字段对我的任务进行排序,我称之为“Seq”。当我使用VBA执行此操作时,似乎什么都不做。但是,在按默认字段(如DueDate)进行排序时,此功能完全正常。有没有人有办法解决吗?我包括我的代码和结果。我正在使用Outlook 2010.提前致谢!

以下是代码:

Sub TestSort()
    Dim ToDoFolder As Folder
    Dim Task As TaskItem
    Dim Sel As Outlook.Items
    Dim InitStartTime As Date
    Dim RemindTime As Date
    Dim FilterString As String

'FORCE CHANGE CURRENT FOLDER TO BE "TO DO LIST" AND VIEW TO BE "Debug (Filter:TEST)"
    Set ToDoFolder = Session.GetDefaultFolder(olFolderToDo)
    Set Application.ActiveExplorer.CurrentFolder = ToDoFolder
    ToDoFolder.Views.Item("Debug (Filter:TEST)").Apply

'AUTOMATICALLY SELECT TASK ITEMS TO BE WORKED WITH

    FilterString = "@SQL=" & ToDoFolder.Views.Item("Debug (Filter:TEST)").Filter
    Set Sel = ToDoFolder.Items.Restrict(FilterString)

'SHOW ORIGINAL SORTING
    Debug.Print Chr(13); "Sort by Original"
    Call CheckSort(Task, Sel)

'SORT TASKS IN MY SELECTION (AND CHECK SORT)

    Sel.Sort "[Seq]", False
    Debug.Print Chr(13); "Sort by Seq Ascending"
    Call CheckSort(Task, Sel)

    Sel.Sort "[Seq]", True
    Debug.Print Chr(13); "Sort by Seq Descending"
    Call CheckSort(Task, Sel)

    Sel.Sort "[DueDate]", False
    Debug.Print Chr(13); "Sort by DueDate Ascending"
    Call CheckSort(Task, Sel)

    Sel.Sort "[DueDate]", True
    Debug.Print Chr(13); "Sort by DueDate Descending"
    Call CheckSort(Task, Sel)

End Sub

Sub CheckSort(Task As Outlook.TaskItem, Sel As Outlook.Items)

'CHECK THAT IT WAS SORTED CORRECTLY

    Debug.Print Chr(13);
    For Each Task In Sel
        Seq = Task.UserProperties.Find("Seq").Value
        Debug.Print Seq & " " & Task.Subject & " " & Task.DueDate
    Next Task
End Sub

结果如下:

Sort by Original

2 Test 1 6/6/2018
4 Test 2 6/8/2018
1 Test 3 6/10/2018
3 Test 4 6/9/2018

Sort by Seq Ascending

2 Test 1 6/6/2018
4 Test 2 6/8/2018
1 Test 3 6/10/2018
3 Test 4 6/9/2018

Sort by Seq Descending

2 Test 1 6/6/2018
4 Test 2 6/8/2018
1 Test 3 6/10/2018
3 Test 4 6/9/2018

Sort by DueDate Ascending

2 Test 1 6/6/2018
4 Test 2 6/8/2018
3 Test 4 6/9/2018
1 Test 3 6/10/2018

Sort by DueDate Descending

1 Test 3 6/10/2018
3 Test 4 6/9/2018
4 Test 2 6/8/2018
2 Test 1 6/6/2018

1 个答案:

答案 0 :(得分:0)

感谢评论,niton。

我已经解决了这个问题。我在这里张贴以防万一有人遇到同样的问题。

我不知道为什么,但在使用Items对象时,这种排序只是不适用于用户定义的字段。但是...我使用Table对象时工作正常! :)查看下面的新代码。

Sub TestSort()
    Dim ToDoFolder As Folder
    Dim Task As TaskItem
    Dim Sel As Outlook.Table
    Dim InitStartTime As Date
    Dim RemindTime As Date
    Dim FilterString As String
    Dim oCol As Outlook.Column

'FORCE CHANGE CURRENT FOLDER TO BE "TO DO LIST" AND VIEW TO BE "Debug (Filter:TEST)"
    Set ToDoFolder = Session.GetDefaultFolder(olFolderToDo)
    Set Application.ActiveExplorer.CurrentFolder = ToDoFolder
    ToDoFolder.Views.Item("Debug (Filter:TEST)").Apply

'AUTOMATICALLY SELECT TASK ITEMS TO BE WORKED WITH

    FilterString = "@SQL=" & ToDoFolder.Views.Item("Debug (Filter:TEST)").Filter
    Set Sel = ToDoFolder.GetTable(FilterString)

'CHECK DEFAULT COLUMNS
    For i = 1 To Sel.Columns.Count
        Set oColumn = Sel.Columns.Item(i)
        Debug.Print oColumn.Name
    Next i

'ADD COLUMN FOR USER-DEFINED FIELD "SEQ"
    Sel.Columns.Add ("Seq")

'SHOW ORIGINAL SORTING
    Debug.Print Chr(13); "Sort by Original"

    Call CheckSortTable(Sel)

'SORT TASKS IN MY SELECTION (AND CHECK SORT)

    Sel.Sort "[Seq]", False
    Debug.Print Chr(13); "Sort by Seq Ascending"
    Call CheckSortTable(Sel)

    Sel.Sort "[Seq]", True
    Debug.Print Chr(13); "Sort by Seq Descending"
    Call CheckSortTable(Sel)

    Sel.Sort "[DueDate]", False
    Debug.Print Chr(13); "Sort by DueDate Ascending"
    Call CheckSortTable(Sel)

    Sel.Sort "[DueDate]", True
    Debug.Print Chr(13); "Sort by DueDate Descending"
    Call CheckSortTable(Sel)

End Sub

Sub CheckSortTable(oTable As Outlook.Table)

    Dim Task As Outlook.Row

'CHECK THAT IT WAS SORTED CORRECTLY

    Debug.Print Chr(13);
    Do Until (oTable.EndOfTable)
        Set Task = oTable.GetNextRow()
        Seq = Task("Seq")
        Debug.Print (Seq & " " & Task("Subject") & " " & Task("DueDate"))
    Loop
End Sub

...和新输出......

EntryID
Subject
CreationTime
LastModificationTime
MessageClass
DueDate
PercentComplete
IsRecurring

Sort by Original

2 Test_1 6/8/2018
4 Test_2 6/10/2018
1 Test_3 6/8/2018
3 Test_4 6/10/2018

Sort by Seq Ascending

1 Test_3 6/8/2018
2 Test_1 6/8/2018
3 Test_4 6/10/2018
4 Test_2 6/10/2018

Sort by Seq Descending

4 Test_2 6/10/2018
3 Test_4 6/10/2018
2 Test_1 6/8/2018
1 Test_3 6/8/2018

Sort by DueDate Ascending

2 Test_1 6/8/2018
1 Test_3 6/8/2018
4 Test_2 6/10/2018
3 Test_4 6/10/2018

Sort by DueDate Descending

4 Test_2 6/10/2018
3 Test_4 6/10/2018
2 Test_1 6/8/2018
1 Test_3 6/8/2018