Handling Inbox.items collection - VSTO Outlook Visual Basic

2

I couldn't find anything regarding a search by conversation ID in the Outlook.Items object collection (corresponding an outlook inbox)

Scenario: we designed a website that requests an email to be searched in outlook client through a table insert

insert into Request(UserID,CID,emailDate,outlookAccount)
values ('myUserID','28603734D8EE4316A59257895B1B4A1A','2020-02-11','myOutlookAccount@outlook.com')

From out VSTO add-in (visual basic) we are recurrently polling for an eventual request on the mentioned table (Request), once there is a request that belongs to the user, we trigger below's function

Private Sub SearchEmail(myConversationID As String, emailDate As String, outlookAccount As String)

        For Each account As Outlook.Account In myOutlookInstance.GetNamespace("MAPI").Accounts
            'i lower de displayname in order to match database information
            If (account.DisplayName.ToLower() = outlookAccount) Then
                Dim mailCollection As Outlook.Items = account.DeliveryStore.GetDefaultFolder(OlDefaultFolders.olFolderInbox).Items
                Dim filter As String = "[ConversationID] =" + "'" + myConversationID + "'"
                Dim matchedMail As Outlook.MailItem = mailCollection.Find(filter)
                matchedMail.Display()
                matchedMail.Save()
            End If
        Next
    End Sub

However I can't make it work. I'm not quite sure but it might be a possibility that filtering by "ConversationID" field won't work. For that reason, I tried with this alternative (it doesn't work either)

    Private Sub SearchEmail(myConversationID As String, emailDate As String, outlookAccount As String)

        For Each account As Outlook.Account In myOutlookInstance.GetNamespace("MAPI").Accounts
            Dim mailCollection As Outlook.Items = account.DeliveryStore.GetDefaultFolder(OlDefaultFolders.olFolderInbox).Items
            Dim eval = False
            For Each item As Object In mailCollection
                If (TypeOf item Is Outlook.MailItem) Then
                    Dim mail As Outlook.MailItem = item
                    Dim myCID As String = mail.ConversationID
                    If (eval = False And (myConversationID = myCID)) Then
                        mail.Display()
                        mail.Save()
                        eval = True
                    End If
                End If
            Next
        Next

    End Sub

Unfortunately (and avoiding the performance and readability issue), this last alternative raises below's issue:

System.InvalidCastException: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE) at Microsoft.Office.Interop.Outlook._MailItem.get_ConversationID()

Could any one help me with this issue?

sql-server
vb.net
outlook
asked on Stack Overflow Feb 11, 2020 by Esquilax • edited Feb 11, 2020 by Dale K

1 Answer

0

After, a while trying to figure out what that issue raised is about, i found that it is raised because i reach a non Outlook.Mailitem object and try to call a method that is not supported for that non Outlook.Mailitem (such as display or save) However, i couldn't get a deffinitive answer regarding a search / filter by conversationID, my definitive guess is that i'm not allowed to. So the best chance i get is to filter as much as i can with the received Date and iterate comparing the Conversation ID property on each item that it hits with the loop

Find below my Work Around:

Private Function SearchEmail(myConversationID As String, emailDate As DateTime, outlookAccount As String)

        For Each account As Outlook.Account In myOutlookInstance.GetNamespace("MAPI").Accounts
            If (account.DisplayName.ToLower() = outlookAccount) Then

                Dim mailCollection As Outlook.Items = account.DeliveryStore.GetDefaultFolder(OlDefaultFolders.olFolderInbox).Items
                mailCollection.Sort("[ReceivedTime]", True)
                Dim myRestrictedMailCollection = mailCollection.Restrict("[ReceivedTime] >= " & "'" & emailDate.ToShortDateString() & "'" & " AND [ReceivedTime] <= " & "'" & emailDate.AddDays(1).ToShortDateString() & "'")
                Dim mail As Outlook.MailItem

                For Each item In myRestrictedMailCollection

                    If (TypeOf item Is Outlook.MailItem) Then

                        mail = item

                        If (myConversationID = mail.ConversationID) Then
                            mail.Display()
                            mail.Save()
                            Return True
                        End If

                    End If

                Next

            End If
        Next

        Return False

    End Function

answered on Stack Overflow Feb 20, 2020 by Esquilax

User contributions licensed under CC BY-SA 3.0