Looping over Outlook mail items in "Sent Items" folder
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.
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
.....
精彩评论