You are declaring oMsg as Outlook.MailItem, but meeting requests are represented as Outlook.MeetingItem objects.
Declare oMsg as a generic Object and check the Class or MessageClass property at run-time to decide how to cast the current item.
Question
Hello all I have the following code that checks for currently unread messages:
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
Now this works just fine with just emails but it seems to crash on the line oMsg = oItems.Item(i) when its trying to process a calender invite email.
The error is this:
Additional information: Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.MailItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063034-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
What would I need to add/and or check in order for it not to crash if its a calender event invite?
Code Update
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
I no longer have an error on the oMsg = oItems.Item(i) but the debug.print output is 16 for both a normal email and calender invite....
La solution
You are declaring oMsg as Outlook.MailItem, but meeting requests are represented as Outlook.MeetingItem objects.
Declare oMsg as a generic Object and check the Class or MessageClass property at run-time to decide how to cast the current item.