Looping over Outlook mail items in “Sent Items” folder

六月ゝ 毕业季﹏ 提交于 2019-12-11 07:33:53

问题


We're trying to access the Sent Items folder in Outlook 2007 (using Exchange) but the test for TypeOf(i) Is Outlook.MailItem in the below code snippet always returns False.

We know we have the right folder because a test for items.Count returns the correct number of mail items.

Inbox messages are fine. If we change the folder from olFolderSentMail to olFolderInbox the test for TypeOf(i) Is Outlook.MailItem passes and it's quite happy to show us the Subject.

Dim app As Outlook.Application = Nothing
Dim ns As Outlook.NameSpace = Nothing
Dim siFolder As Outlook.Folder = Nothing
Dim items As Outlook.Items = Nothing

app = New Outlook.Application()
ns = app.Session


siFolder = CType(ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail), Outlook.Folder)

items = siFolder.Items

MsgBox(items.Count)

For Each i In items

    If TypeOf (i) Is Outlook.MailItem Then
        Dim mailitem As Outlook.MailItem
        mailitem = CType(i, Outlook.MailItem)
        MsgBox(mailitem.Subject)
    Else
        MsgBox("not a mailitem")
    End If
Next

Update

@Rob's answer below, yes, definitely has helped. But I'm still puzzled. The crucial thing @Rob's code is doing is testing for .MessageClass = "IPM.Note". If I include that then the later test for TypeOf x Is MailItem succeeds. If I replace @Rob's test for .MessageClass = "IPM.Note" with If True Then then the same code still executes but the later test for Is MailItem fails. It's as if just testing for the .MessageClass automagically resolves the object into a MailItem.

Furthermore the Sent Items don't contain any meeting requests so the test would seem to be unnecessary anyway.


回答1:


This should get you going ...

....

Dim oSent As Outlook.MAPIFolder = oNS.GetFolderFromID(gSentEntryID, gSentStoreID)


 Dim oItems As Outlook.Items = oSent.Items

 For i as Integer = 1 To oItems.Count
   'Test to make sure item is a mail item and not a meeting request.
    If oItems.Item(i).MessageClass = "IPM.Note" Then

        If TypeOf oItems.Item(i) Is Microsoft.Office.Interop.Outlook.MailItem Then

             .....


来源:https://stackoverflow.com/questions/5044906/looping-over-outlook-mail-items-in-sent-items-folder

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