如何从邮件项目返回任务状态?

时间:2018-10-16 21:42:24

标签: vba outlook-vba

我有一个返回邮件项目各种属性的代码。我正在尝试将“任务状态”添加到我的报告中。

我收到运行时错误“ 438”“对象不支持此属性或方法”。我正在尝试提取Outlook中的小标记是否已完成(也称为选中)。

这是我到目前为止所拥有的:

        For Each currentTask In currentItem.Tasks
            Debug.Print currentTask.Status
            Report = Report & currentTask.Status
        Next

这是此较大子项的一部分:

Private Sub GetAllEmailsInFolder(CurrentFolder As Outlook.Folder, Report As String)
    Dim currentItem
    Dim attachment As attachment
    Dim currentMail As MailItem
    Dim currenTask As TaskItem

    Report = Report & "Folder Name: " & CurrentFolder.Name & " (Store: " & CurrentFolder.Store.DisplayName & ")" & " (Date of report: " _
        & Date & ")" & vbCrLf & "Subject Name|Categories|Attachment Count|Task Status|Attachment Name(s)" & vbCrLf
    For Each currentItem In CurrentFolder.Items
        Report = Report & currentItem.Subject & "|"
        Report = Report & currentItem.Categories & "|"
        Report = Report & currentItem.Attachments.Count & "|"
        'need help here
        For Each currentTask In currentItem.Tasks
            Debug.Print currentTask.Status
            Report = Report & currentTask.Status
        Next
        '
        For Each attachment In currentItem.Attachments
            Debug.Print attachment.FileName
            Report = Report & attachment.FileName & ","
        Next

        Report = Report & vbCrLf
    Next

End Sub

3 个答案:

答案 0 :(得分:0)

使用MailItem.FlagDueBy / FlagIcon / FlagRequest / FlagStatus / IsMarkedAsTask / TaskCompletedDate / TaskDueDate / TaskStartDate / TaskSubject / ToDoTaskOrdinal属性。

答案 1 :(得分:0)

在没有更好的解决方案的情况下,您可以使用PropertyAccessor来实现此目的。

我目前无法为您提供代码段,但参考页[1]上有说明性示例。

您要查找的属性标签是0x8025,带有DASL http://schemas.microsoft.com/mapi/id/{00062003-0000-0000-C000-000000000046}/81010003

您可以使用OutlookSpy来确定实际属性的属性标签(感谢本技巧的[2]。)

[1] https://docs.microsoft.com/en-us/office/vba/api/outlook.propertyaccessor

[2] How can I get task-specific properties from a MailItem

编辑1

Private Function GetStatus(objItem As Object) As OlTaskStatus
    Dim oPA As Outlook.PropertyAccessor

    ' MAPI-level access required to get the "status" property of a Mail Item object.
    Set oPA = objItem.PropertyAccessor
    GetStatus = oPA.GetProperty("http://schemas.microsoft.com/mapi/id/{00062003-0000-0000-C000-000000000046}/81010003")

    Set oPA = Nothing
End Function

答案 2 :(得分:0)

邮件项具有.FlagStatus属性。

Option Explicit ' Consider this mandatory
' Tools | Options | Editor tab
' Require Variable Declaration
' If desperate declare as Variant

Private Sub GetAllEmailsInFolder(CurrentFolder As outlook.Folder, Report As String)

    ' Code for flags not reliable in IMAP accounts
    
    Dim currentItem As Object
    Dim attachment As attachment
    Dim currentMail As MailItem

    'Dim currenTask As TaskItem   ' <--- missing Option Explicit?
    Dim currentTask As TaskItem
    
    Dim currentFolderItems As Items
    
    Report = Report & "Folder Name: " & CurrentFolder.Name & " (Store: " & CurrentFolder.Store.DisplayName & ")" & " (Date of report: " _
        & Date & ")" & vbCrLf & "Subject Name|Categories|Attachment Count|Task Status|Attachment Name(s)" & vbCrLf
    
    Set currentFolderItems = CurrentFolder.Items
    
    For Each currentItem In currentFolderItems
        
        If currentItem.Class = olMail Then
        
            Set currentMail = currentItem
            
            With currentMail
            
                Debug.Print .Subject
                
                Report = Report & .Subject & "|"
                Report = Report & .categories & "|"
                Report = Report & .Attachments.Count & "|"
                
                ' No longer in current documentation
                ' https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2010/bb644164(v=office.14)
                ' Still could be good for decades
                '   0 - olNoFlag
                '   1 - olFlagComplete
                '   2 - olFlagMarked
                Debug.Print ".FlagStatus.: " & .FlagStatus
                
                'https://docs.microsoft.com/en-us/office/vba/api/outlook.mailitem.flagrequest
                Debug.Print ".FlagRequest: " & .FlagRequest
                
                Report = Report & .FlagStatus
                
                For Each attachment In .Attachments
                    Debug.Print attachment.Filename
                    Report = Report & attachment.Filename & ","
                Next
                
            End With
            
            Report = Report & vbCrLf
            Debug.Print Report
            
        ElseIf currentItem.Class = olTask Then
        
            Set currentTask = currentItem
            
            With currentTask
            
                Report = Report & .Subject & "|"
                Report = Report & .categories & "|"
                Report = Report & .Attachments.Count & "|"
                
                Debug.Print ".Status.....: " & .Status

                Report = Report & .Status
                
                For Each attachment In .Attachments
                    Debug.Print attachment.Filename
                    Report = Report & attachment.Filename & ","
                Next
                
            End With
            
            Report = Report & vbCrLf
            Debug.Print Report
        
        Else
            Debug.Print "neither a mailitem nor a taskitem"
            
        End If
        
        Set currentItem = Nothing
        Set currentTask = Nothing
        Set currentMail = Nothing

    Next

End Sub


Private Sub test()

    Dim currFolder As Folder
    Dim reportStr As String
    
    Set currFolder = ActiveExplorer.CurrentFolder
    reportStr = "FlagStaus on mailitems: "
    
    GetAllEmailsInFolder currFolder, reportStr
    
End Sub