Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing 2 objects

Status
Not open for further replies.

utahjzz98

Programmer
Jan 9, 2003
69
US
I have a program that goes through an Outlook folder and counts all of the email items in that folder and seperates it by date. However, I am running into type mismatch error if the program runs across a meeting request or any other object in the folder that's not a mail item. I tried using the CType function to compare the objects, but I wasn't able to get it to work (guessing it's not supported in VBA?). Any ideas for how to compare each item in the folder and determine if it's an email item, and if not disregard it?

Code:
For Each x In objFolder.Items
  Set objEmails(i) = x
  tmpDate = DateValue(objEmails(i).ReceivedTime)
  intDay = Day(tmpDate)
  
  If intDay >= 1 And intDay <= 31 Then
    If g_intMonth = Month(objEmails(i).ReceivedTime) And
      g_intYear = Year(objEmails(i).ReceivedTime) Then
      intCount(intDay) = intCount(intDay) + 1
    End If
  End If
Next
 
Test the x.Class property.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi utahjzz98,

The usual way to check the type of an object is ..
Code:
[blue]If TypeOf x Is MailItem Then[/blue]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
TonyJollans,

Thanks a lot, that's what I was looking for.
 
I didn't post that part of my code, but I am filling a collection with mailitem objects. The problem was when I went iterated through the folders I got a type mismatch error when it came across something that wasn't a mailitem. i.e. A meeting request. What I needed was something to determine what type of object I was currently looking at so I could determine if it was a mailitem or some other object. Which is what the TypeOf function helped me accomplish. Below is how my code looks now and it's working properly.

Code:
For Each x In objFolder.Items
  If TypeOf x Is MailItem Then
    Set objEmails(i) = x
    tmpDate = DateValue(objEmails(i).ReceivedTime)
    intDay = Day(tmpDate)
             
    If intDay >= 1 And intDay <= 31 Then
      If g_intMonth = Month(objEmails(i).ReceivedTime) And g_intYear = Year(objEmails(i).ReceivedTime) Then
        intCount(intDay) = intCount(intDay) + 1
      End If
    End If         
 End If
Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top