我们正在尝试访问 Outlook 2007(使用Exchange)中的已发送邮件文件夹,但以下代码段中的TypeOf(i) Is Outlook.MailItem
测试始终返回假
我们知道我们有正确的文件夹,因为items.Count
的测试会返回正确数量的邮件。
收件箱信息很好。如果我们将文件夹从olFolderSentMail
更改为olFolderInbox
,则TypeOf(i) Is Outlook.MailItem
的测试通过,并且非常乐意向我们展示主题。
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
@ Rob的回答如下,是的,绝对有帮助。但我仍然感到困惑。 @ Rob的代码正在做的关键是测试.MessageClass = "IPM.Note"
。如果我包含那个,那么TypeOf x Is MailItem
的后续测试会成功。如果我用.MessageClass = "IPM.Note"
替换If True Then
的@ Rob测试,则相同的代码仍然会执行,但后来的Is MailItem测试失败。好像只是测试.MessageClass
会自动将对象解析为MailItem。
此外,已发送的项目不包含任何会议请求,因此无论如何都不需要进行测试。
答案 0 :(得分:2)
这应该让你去......
...
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
.....