InvalidCastException with Outlook 2003

  • Thread starter Thread starter Peter van der Veen
  • Start date Start date
P

Peter van der Veen

Hi

In my VB.net application i got on different times (and not allways) an
System.InvalidCastException.

The line of code is:

objAppointmentItem = objItems.GetFirst

Does anyone know what to do???


Peter
 
Hi,

Enclose it in a try catch block. I would also turn option strict on.
Make sure you use ctype or directcast to convert between types.

Ken
--------------
 
Peter,
In addition to the other comments.

Use the TypeOf operator to ensure that you have an object of the type you
are expecting...

Dim objAppointmentItem as AppointmentItem

Dim obj As Object = objItems.GetFirst()

If TypeOf obj Is AppointmentItem Then
objAppointmentItem = DirectCast(obj, AppointmentItem)
' do stuff with objAppointmentItem
End If

Hope this helps
Jay
 
Thx, it works now.

Outlook 2003 is different than 2000. With 2000 i don't have had this
error
 
Peter,
Remember that a folder may have multiple kind of Items in the folder. I
suspect in Outlook 2000 you only had Appointment Items in the folder, hence
you code works. Where as with Outlook 2003 you now have a second kind of
item in that folder, hence the exception.

Also using SetColumns causes the type of item to change...

Hope this helps
Jay
 
Back
Top