VB.net Outlook检查邮件和日历事件

时间:2014-04-16 15:52:36

标签: vb.net outlook

大家好,我有以下代码检查当前未读的消息:

 oInbox = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox).Parent.Folders(pair.Key)
 oItems = oInbox.Items
 oItems = oItems.Restrict("[Unread] = true")

 Dim oMsg As Outlook.MailItem
 For i As Integer = 1 To oItems.Count
    oMsg = oItems.Item(i)
    Dim tmpString As String = ""
    tmpString = oMsg.Subject & "^" & oMsg.SenderName & "^" & oMsg.ReceivedTime & "^" & oMsg.Body & "^" & oMsg.EntryID
    outlookFoldersMail.Add(dictionaryNum, tmpString)
    oMsg = Nothing
    dictionaryNum += 1
 Next

现在这只适用于电子邮件,但是当它试图处理日历邀请电子邮件时,它似乎在 oMsg = oItems.Item(i)上崩溃。

错误是这样的:

附加信息:无法将“System .__ ComObject”类型的COM对象强制转换为接口类型“Microsoft.Office.Interop.Outlook.MailItem”。此操作失败,因为由于以下错误,IID为“{00063034-0000-0000-C000-000000000046}”的接口的COM组件上的QueryInterface调用失败:不支持此类接口(HRESULT异常:0x80004002(E_NOINTERFACE))

我需要添加/和/或检查一下,如果它的日历事件邀请它不会崩溃?

代码更新

oInbox = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox).Parent.Folders(pair.Key)      oItems = oInbox.Items      oItems = oItems.Restrict(“[Unread] = true”)

 Dim oMsg As Object

 For i As Integer = 1 To oItems.Count
    Debug.Print(oItems.Class)
    oMsg = oItems.Item(i)
    Dim tmpString As String = ""
    tmpString = oMsg.Subject & "^" & oMsg.SenderName & "^" & oMsg.ReceivedTime & "^" & oMsg.Body & "^" & oMsg.EntryID
    outlookFoldersMail.Add(dictionaryNum, tmpString)
    oMsg = Nothing
    dictionaryNum += 1
 Next

oMsg = oItems.Item(i)上不再出现错误,但 debug.print 输出 16 一个普通的电子邮件和日历邀请....

1 个答案:

答案 0 :(得分:0)

您将oMsg声明为Outlook.MailItem,但会议请求表示为Outlook.MeetingItem对象。

将oMsg声明为通用Object,并在运行时检查Class或MessageClass属性,以决定如何转换当前项。