Link to home
Start Free TrialLog in
Avatar of dwe0608
dwe0608Flag for Australia

asked on

vb.net - Outlook - Counting the number of items in a conversation

Merry Christmas guys ...

I am implementing a function will will return the number of items in an Outlook Conversation - I have cobbled the following together and whilst no error is thrown - it doesnt seem to return the correct count:

    Function GetConv(mailItem As Outlook.MailItem)
        Dim Conversation As Outlook.Conversation ' Get the conversation
        Dim Items As Outlook.SimpleItems
        Dim expMessage$

        '   // If Item = a MailItem.
        Try
            If TypeOf (mailItem) Is Outlook.MailItem Then
                Conversation = mailItem.GetConversation
                If Not IsNothing(Conversation) Then
                    Items = Conversation.GetChildren(mailItem)
                    GetConv = Items.Count
                End If
            Else
                GetConv = 0
            End If

        Catch ex As SystemException
            expMessage = ex.Message
            MsgBox(expMessage)
            GetConv = 0
        End Try

    End Function

Open in new window


Can someone assist to ensure that the correct count of the items in the conversation are returned?

MTIA

DWE
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dwe0608

ASKER

Hi Bill, Thanks for the direction ... the following function now works and returns the correct number of children in the conversation ...

I haven't done a lot of VB.Net stuff - in VB6 I would have disposed of the objects by setting them to nothing (ie set Conversation = nothing) ...  is it still good practice to do that ... (ie maybe like this conversation = nothing) - I notice that the set assignment is no longer supported...

    Function GetConv(mailItem As Outlook.MailItem) As Integer
        Dim Conversation As Outlook.Conversation
        Dim oTable As Outlook.Table

        Dim expMessage$

        '   // If Item = a MailItem.
        Try
            If TypeOf (mailItem) Is Outlook.MailItem Then
                Conversation = mailItem.GetConversation
                If Not IsNothing(Conversation) Then
                    oTable = Conversation.GetTable() '.GetChildren(mailItem)
                    GetConv = oTable.GetRowCount()
                Else
                    GetConv = 0
                End If
            Else
                GetConv = 0
            End If

        Catch ex As SystemException
            expMessage = ex.Message
            MsgBox(expMessage)
            GetConv = 0
        End Try

    End Function

Open in new window

Avatar of dwe0608

ASKER

Thanks for the help ... Merry Christmas and Happy New Year to you and yours.
Avatar of Bill Prew
Bill Prew

Honestly, I see a lot of discussion from time to time about disposing of objects or not.  I still often do it, but have never found a single definitive answer to the question.  There are arguments for and against and hard to sort out fact from fiction sometimes.  In theory any variables that are scoped to a function or procedure are freed up after that procedure ends, but the runtime engine decided when the space from those objects gets released ("garbage collection"). Within a procedure, if I am going to reuse the same variable to reference multiple occurrences of an object type (one at a time, in a loop say) I often will set the variable to Nothing between uses, just to make sure I don't pick up the prior value somehow.  Beyond that it seems to be somewhat a personal choice, from my perspective...


»bp
Avatar of dwe0608

ASKER

Thanks for that Bill ... I agree with what you say ... I don't program enough nowadays to have an opinion on it, but there are some procedures I have written where it just doesn't seem natural to leave the objects seemingly alive when the function or sub exits ...