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
LVL 1
dwe0608Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
Looking at this link on the Conversation object:

Conversation Object (Outlook)

It looks like you may need to use the .Get Table() method to obtain the count.  I haven't tried it in VBA here but wanted to share the link, and a chunk of the code...

// Obtain a Conversation object. 
Outlook.Conversation conv =  mailItem.GetConversation(); 
// Check for null Conversation. 
if (conv != null) 
{ 
// Obtain Table that contains rows 
// for each item in the conversation. 
Outlook.Table table = conv.GetTable(); 
Debug.WriteLine("Conversation Items Count: " + table.GetRowCount().ToString()); 
Debug.WriteLine("Conversation Items from Table:"); 
while (!table.EndOfTable) 
{ 
Outlook.Row nextRow = table.GetNextRow(); 
Debug.WriteLine(nextRow["Subject"] + " Modified: " + nextRow["LastModificationTime"]); 
} 

Open in new window


»bp

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dwe0608Author Commented:
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

dwe0608Author Commented:
Thanks for the help ... Merry Christmas and Happy New Year to you and yours.
Bill PrewIT / Software Engineering ConsultantCommented:
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
dwe0608Author Commented:
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 ...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.